fix playing indicator
This commit is contained in:
parent
262548e77c
commit
4b8386b716
1 changed files with 9 additions and 7 deletions
|
@ -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<Boolean> = 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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue