From 4b8386b7160446f47df8c8fa79cdb34a8b461862 Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Tue, 11 Mar 2025 01:31:34 -0400 Subject: [PATCH] fix playing indicator --- .../java/com/example/gyromin/MainActivity.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/example/gyromin/MainActivity.kt b/app/src/main/java/com/example/gyromin/MainActivity.kt index 1aa6ab4..0d1717b 100644 --- a/app/src/main/java/com/example/gyromin/MainActivity.kt +++ b/app/src/main/java/com/example/gyromin/MainActivity.kt @@ -13,6 +13,8 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button import androidx.compose.material3.Scaffold import androidx.compose.material3.Text +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import com.example.gyromin.ui.theme.GyrominTheme @@ -21,7 +23,7 @@ import kotlin.math.sin class MainActivity : ComponentActivity() { private lateinit var Track: AudioTrack - private var isPlaying: Boolean = false + private var isPlaying: MutableState = mutableStateOf(false) private val Fs: Int = 44100 private val buffLength: Int = AudioTrack.getMinBufferSize(Fs, 4, 2) @@ -37,7 +39,7 @@ class MainActivity : ComponentActivity() { horizontalAlignment = Alignment.CenterHorizontally ) { Button(onClick = { - if (!isPlaying) + if (!isPlaying.value) // Create a new thread to play the audio. // 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( - if (isPlaying) "Playing" else "Stopped", + if (isPlaying.value) "Playing" else "Stopped", modifier = Modifier.padding(padding) ) } @@ -85,7 +87,7 @@ class MainActivity : ComponentActivity() { val twopi: Double = 8.0 * atan(1.0) var phase = 0.0 - while (isPlaying) { + while (isPlaying.value) { for (i in 0 until buffLength) { frame_out[i] = (amplitude * sin(phase)).toInt().toShort() phase += twopi * frequency / Fs @@ -99,12 +101,12 @@ class MainActivity : ComponentActivity() { private fun startPlaying() { Track.play() - isPlaying = true + isPlaying.value = true } private fun stopPlaying() { - if (isPlaying) { - isPlaying = false + if (isPlaying.value) { + isPlaying.value = false // Stop playing the audio data and release the resources Track.stop() Track.release()