added stop button and info to put in minutes
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
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user