added stop button and info to put in minutes

This commit is contained in:
pi
2025-10-29 13:20:31 +01:00
parent 4b4405a601
commit 6c3a36efe3
2 changed files with 42 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.EditText
class MainActivity : Activity() {
private lateinit var soundPool: SoundPool
@@ -19,7 +20,8 @@ class MainActivity : Activity() {
setContentView(R.layout.activity_main)
val input = findViewById<EditText>(R.id.timerInput)
val btn = findViewById<Button>(R.id.startBtn)
val btnStart = findViewById<Button>(R.id.startBtn)
val btnStop = findViewById<Button>(R.id.stopBtn)
val attrs = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
@@ -33,20 +35,29 @@ class MainActivity : Activity() {
soundId = soundPool.load(this, R.raw.sound, 1)
btn.setOnClickListener {
btnStart.setOnClickListener {
val min = input.text.toString().toIntOrNull() ?: 0
startLoopingSound(min)
}
btnStop.setOnClickListener {
stopSound()
}
}
private fun startLoopingSound(minutes: Int) {
// Starte Sound in Schleife
stopSound() // sicherstellen, dass kein alter Loop läuft
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
if (minutes > 0) {
handler.postDelayed({
soundPool.stop(streamId)
}, minutes * 60_000L)
handler.postDelayed({ stopSound() }, minutes * 60_000L)
}
}
private fun stopSound() {
if (streamId != 0) {
soundPool.stop(streamId)
streamId = 0
}
}