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({
handler.postDelayed({ stopSound() }, minutes * 60_000L)
}
}
private fun stopSound() {
if (streamId != 0) {
soundPool.stop(streamId)
}, minutes * 60_000L)
streamId = 0
}
}

View File

@@ -6,12 +6,26 @@
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/childDescription"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Timer in Minutes:"
android:textSize="14sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<EditText
android:id="@+id/timerInput"
android:hint="Sleep Timer (Minuten)"
android:hint="empty means endless"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Space
android:layout_width="match_parent"
android:layout_height="30dip" />
<Button
android:id="@+id/startBtn"
@@ -19,4 +33,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Space
android:layout_width="match_parent"
android:layout_height="5dip" />
<Button
android:id="@+id/stopBtn"
android:text="Stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>