fix playing indicator

This commit is contained in:
Breval Ferrari 2025-03-11 01:31:34 -04:00
parent 262548e77c
commit 4b8386b716
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E

View file

@ -13,6 +13,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import com.example.gyromin.ui.theme.GyrominTheme import com.example.gyromin.ui.theme.GyrominTheme
@ -21,7 +23,7 @@ import kotlin.math.sin
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private lateinit var Track: AudioTrack private lateinit var Track: AudioTrack
private var isPlaying: Boolean = false private var isPlaying: MutableState<Boolean> = mutableStateOf(false)
private val Fs: Int = 44100 private val Fs: Int = 44100
private val buffLength: Int = AudioTrack.getMinBufferSize(Fs, 4, 2) private val buffLength: Int = AudioTrack.getMinBufferSize(Fs, 4, 2)
@ -37,7 +39,7 @@ class MainActivity : ComponentActivity() {
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
Button(onClick = { Button(onClick = {
if (!isPlaying) if (!isPlaying.value)
// Create a new thread to play the audio. // Create a new thread to play the audio.
// Performing intensive operations and computations on the main UI thread, // Performing intensive operations and computations on the main UI thread,
@ -56,7 +58,7 @@ class MainActivity : ComponentActivity() {
Text("Play / Pause", modifier = Modifier.padding(padding)) Text("Play / Pause", modifier = Modifier.padding(padding))
} }
Text( Text(
if (isPlaying) "Playing" else "Stopped", if (isPlaying.value) "Playing" else "Stopped",
modifier = Modifier.padding(padding) modifier = Modifier.padding(padding)
) )
} }
@ -85,7 +87,7 @@ class MainActivity : ComponentActivity() {
val twopi: Double = 8.0 * atan(1.0) val twopi: Double = 8.0 * atan(1.0)
var phase = 0.0 var phase = 0.0
while (isPlaying) { while (isPlaying.value) {
for (i in 0 until buffLength) { for (i in 0 until buffLength) {
frame_out[i] = (amplitude * sin(phase)).toInt().toShort() frame_out[i] = (amplitude * sin(phase)).toInt().toShort()
phase += twopi * frequency / Fs phase += twopi * frequency / Fs
@ -99,12 +101,12 @@ class MainActivity : ComponentActivity() {
private fun startPlaying() { private fun startPlaying() {
Track.play() Track.play()
isPlaying = true isPlaying.value = true
} }
private fun stopPlaying() { private fun stopPlaying() {
if (isPlaying) { if (isPlaying.value) {
isPlaying = false isPlaying.value = false
// Stop playing the audio data and release the resources // Stop playing the audio data and release the resources
Track.stop() Track.stop()
Track.release() Track.release()