58 lines
1.6 KiB
Kotlin
58 lines
1.6 KiB
Kotlin
package com.example.hairdryer
|
|
|
|
import android.app.Activity
|
|
import android.media.AudioAttributes
|
|
import android.media.SoundPool
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.widget.Button
|
|
import android.widget.EditText
|
|
class MainActivity : Activity() {
|
|
|
|
private lateinit var soundPool: SoundPool
|
|
private var soundId = 0
|
|
private var streamId = 0
|
|
private val handler = Handler()
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_main)
|
|
|
|
val input = findViewById<EditText>(R.id.timerInput)
|
|
val btn = findViewById<Button>(R.id.startBtn)
|
|
|
|
val attrs = AudioAttributes.Builder()
|
|
.setUsage(AudioAttributes.USAGE_MEDIA)
|
|
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
|
.build()
|
|
|
|
soundPool = SoundPool.Builder()
|
|
.setMaxStreams(1)
|
|
.setAudioAttributes(attrs)
|
|
.build()
|
|
|
|
soundId = soundPool.load(this, R.raw.sound, 1)
|
|
|
|
btn.setOnClickListener {
|
|
val min = input.text.toString().toIntOrNull() ?: 0
|
|
startLoopingSound(min)
|
|
}
|
|
}
|
|
|
|
private fun startLoopingSound(minutes: Int) {
|
|
// Starte Sound in Schleife
|
|
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
|
|
|
|
if (minutes > 0) {
|
|
handler.postDelayed({
|
|
soundPool.stop(streamId)
|
|
}, minutes * 60_000L)
|
|
}
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
super.onDestroy()
|
|
soundPool.release()
|
|
}
|
|
}
|