changed to start/stopp
This commit is contained in:
@@ -7,6 +7,7 @@ import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
|
||||
class MainActivity : Activity() {
|
||||
|
||||
@@ -14,14 +15,17 @@ class MainActivity : Activity() {
|
||||
private var soundId = 0
|
||||
private var streamId = 0
|
||||
private val handler = Handler()
|
||||
private var isPlaying = false
|
||||
private var remainingMinutes = 0
|
||||
private var countdownRunnable: Runnable? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
val status = findViewById<TextView>(R.id.statusText)
|
||||
val input = findViewById<EditText>(R.id.timerInput)
|
||||
val btnStart = findViewById<Button>(R.id.startBtn)
|
||||
val btnStop = findViewById<Button>(R.id.stopBtn)
|
||||
val btn = findViewById<Button>(R.id.mainBtn)
|
||||
|
||||
val attrs = AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
@@ -35,32 +39,57 @@ class MainActivity : Activity() {
|
||||
|
||||
soundId = soundPool.load(this, R.raw.sound, 1)
|
||||
|
||||
btnStart.setOnClickListener {
|
||||
val min = input.text.toString().toIntOrNull() ?: 0
|
||||
startLoopingSound(min)
|
||||
}
|
||||
|
||||
btnStop.setOnClickListener {
|
||||
stopSound()
|
||||
btn.setOnClickListener {
|
||||
if (isPlaying) {
|
||||
stopSound(btn, status)
|
||||
} else {
|
||||
val min = input.text.toString().toIntOrNull() ?: 0
|
||||
startSound(min, btn, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startLoopingSound(minutes: Int) {
|
||||
stopSound() // sicherstellen, dass kein alter Loop läuft
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
|
||||
private fun startSound(minutes: Int, btn: Button, status: TextView) {
|
||||
stopSound(btn, status) // sicherstellen, dass nichts doppelt läuft
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
|
||||
isPlaying = true
|
||||
btn.text = "Stop"
|
||||
|
||||
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() {
|
||||
if (streamId != 0) {
|
||||
private fun stopSound(btn: Button, status: TextView) {
|
||||
if (isPlaying) {
|
||||
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() {
|
||||
super.onDestroy()
|
||||
soundPool.release()
|
||||
|
||||
@@ -2,12 +2,32 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp">
|
||||
|
||||
<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:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -28,19 +48,9 @@
|
||||
android:layout_height="30dip" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/startBtn"
|
||||
android:id="@+id/mainBtn"
|
||||
android:text="Start"
|
||||
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"/>
|
||||
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user