changed to start/stopp

This commit is contained in:
pi
2025-10-29 13:47:08 +01:00
parent 6c3a36efe3
commit 1ca9f10658
2 changed files with 70 additions and 31 deletions

View File

@@ -7,6 +7,7 @@ import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.TextView
class MainActivity : Activity() { class MainActivity : Activity() {
@@ -14,14 +15,17 @@ class MainActivity : Activity() {
private var soundId = 0 private var soundId = 0
private var streamId = 0 private var streamId = 0
private val handler = Handler() private val handler = Handler()
private var isPlaying = false
private var remainingMinutes = 0
private var countdownRunnable: Runnable? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
val status = findViewById<TextView>(R.id.statusText)
val input = findViewById<EditText>(R.id.timerInput) val input = findViewById<EditText>(R.id.timerInput)
val btnStart = findViewById<Button>(R.id.startBtn) val btn = findViewById<Button>(R.id.mainBtn)
val btnStop = findViewById<Button>(R.id.stopBtn)
val attrs = AudioAttributes.Builder() val attrs = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA) .setUsage(AudioAttributes.USAGE_MEDIA)
@@ -35,32 +39,57 @@ class MainActivity : Activity() {
soundId = soundPool.load(this, R.raw.sound, 1) soundId = soundPool.load(this, R.raw.sound, 1)
btnStart.setOnClickListener { btn.setOnClickListener {
val min = input.text.toString().toIntOrNull() ?: 0 if (isPlaying) {
startLoopingSound(min) stopSound(btn, status)
} } else {
val min = input.text.toString().toIntOrNull() ?: 0
btnStop.setOnClickListener { startSound(min, btn, status)
stopSound() }
} }
} }
private fun startLoopingSound(minutes: Int) { private fun startSound(minutes: Int, btn: Button, status: TextView) {
stopSound() // sicherstellen, dass kein alter Loop läuft stopSound(btn, status) // sicherstellen, dass nichts doppelt läuft
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
isPlaying = true
btn.text = "Stop"
if (minutes > 0) { if (minutes > 0) {
handler.postDelayed({ stopSound() }, minutes * 60_000L) remainingMinutes = minutes
updateStatus(status)
countdownRunnable = object : Runnable {
override fun run() {
remainingMinutes--
if (remainingMinutes > 0) {
updateStatus(status)
handler.postDelayed(this, 60_000L)
} else {
stopSound(btn, status)
}
}
}
handler.postDelayed(countdownRunnable!!, 60_000L)
} else {
status.text = "Läuft: ∞"
} }
} }
private fun stopSound() { private fun stopSound(btn: Button, status: TextView) {
if (streamId != 0) { if (isPlaying) {
soundPool.stop(streamId) soundPool.stop(streamId)
streamId = 0 isPlaying = false
remainingMinutes = 0
handler.removeCallbacks(countdownRunnable ?: Runnable { })
status.text = "Gestoppt"
btn.text = "Start"
} }
} }
private fun updateStatus(status: TextView) {
status.text = "Läuft: ${remainingMinutes} min"
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
soundPool.release() soundPool.release()

View File

@@ -2,12 +2,32 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:padding="20dp"> android:padding="20dp">
<TextView <TextView
android:id="@+id/childDescription" android:id="@+id/parentDeviceTitle"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_vertical"
android:text="Dunstabzugshaube/Hairdryer"
android:textSize="25dp" />
<Space
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:id="@+id/statusText"
android:gravity="center_horizontal"
android:text="Bereit"
android:textSize="24sp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Info"
android:gravity="center" android:gravity="center"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@@ -28,19 +48,9 @@
android:layout_height="30dip" /> android:layout_height="30dip" />
<Button <Button
android:id="@+id/startBtn" android:id="@+id/mainBtn"
android:text="Start" android:text="Start"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
<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> </LinearLayout>