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.os.Handler
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
|
|
||||||
class MainActivity : Activity() {
|
class MainActivity : Activity() {
|
||||||
|
|
||||||
private lateinit var soundPool: SoundPool
|
private lateinit var soundPool: SoundPool
|
||||||
@@ -19,7 +20,8 @@ class MainActivity : Activity() {
|
|||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
val input = findViewById<EditText>(R.id.timerInput)
|
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()
|
val attrs = AudioAttributes.Builder()
|
||||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||||
@@ -33,20 +35,29 @@ class MainActivity : Activity() {
|
|||||||
|
|
||||||
soundId = soundPool.load(this, R.raw.sound, 1)
|
soundId = soundPool.load(this, R.raw.sound, 1)
|
||||||
|
|
||||||
btn.setOnClickListener {
|
btnStart.setOnClickListener {
|
||||||
val min = input.text.toString().toIntOrNull() ?: 0
|
val min = input.text.toString().toIntOrNull() ?: 0
|
||||||
startLoopingSound(min)
|
startLoopingSound(min)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btnStop.setOnClickListener {
|
||||||
|
stopSound()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startLoopingSound(minutes: Int) {
|
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
|
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
|
||||||
|
|
||||||
if (minutes > 0) {
|
if (minutes > 0) {
|
||||||
handler.postDelayed({
|
handler.postDelayed({ stopSound() }, minutes * 60_000L)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopSound() {
|
||||||
|
if (streamId != 0) {
|
||||||
soundPool.stop(streamId)
|
soundPool.stop(streamId)
|
||||||
}, minutes * 60_000L)
|
streamId = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,26 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:padding="20dp">
|
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
|
<EditText
|
||||||
android:id="@+id/timerInput"
|
android:id="@+id/timerInput"
|
||||||
android:hint="Sleep Timer (Minuten)"
|
android:hint="empty means endless"
|
||||||
android:inputType="number"
|
android:inputType="number"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"/>
|
android:layout_height="wrap_content"/>
|
||||||
|
<Space
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="30dip" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/startBtn"
|
android:id="@+id/startBtn"
|
||||||
@@ -19,4 +33,14 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"/>
|
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>
|
</LinearLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user