integrated start/stop button to ready or stopped button
This commit is contained in:
@@ -6,7 +6,6 @@ import android.media.AudioAttributes
|
||||
import android.media.SoundPool
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
|
||||
@@ -26,7 +25,6 @@ class MainActivity : Activity() {
|
||||
|
||||
val status = findViewById<TextView>(R.id.statusText)
|
||||
val input = findViewById<EditText>(R.id.timerInput)
|
||||
val btn = findViewById<Button>(R.id.mainBtn)
|
||||
|
||||
val attrs = AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
@@ -40,64 +38,59 @@ class MainActivity : Activity() {
|
||||
|
||||
soundId = soundPool.load(this, R.raw.sound, 1)
|
||||
|
||||
// Initial: Start-Zustand (grün)
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
status.setBackgroundColor(Color.LTGRAY)
|
||||
status.setTextColor(Color.BLACK)
|
||||
// Anfangszustand
|
||||
updateUI(status, "Ready", Color.LTGRAY)
|
||||
|
||||
btn.setOnClickListener {
|
||||
// Das Statusfeld ist jetzt klickbar
|
||||
status.setOnClickListener {
|
||||
if (isPlaying) {
|
||||
stopSound(btn, status)
|
||||
stopSound(status)
|
||||
} else {
|
||||
val min = input.text.toString().toIntOrNull() ?: 0
|
||||
startSound(min, btn, status)
|
||||
startSound(min, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startSound(minutes: Int, btn: Button, status: TextView) {
|
||||
stopSound(btn, status) // sicherstellen, dass nichts doppelt läuft
|
||||
private fun startSound(minutes: Int, status: TextView) {
|
||||
stopSound(status)
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f)
|
||||
isPlaying = true
|
||||
btn.text = "Stop"
|
||||
btn.setBackgroundColor(Color.RED)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
|
||||
status.setBackgroundColor(Color.GREEN)
|
||||
status.text = if (minutes > 0) "Läuft: ${minutes} min" else "Läuft: ∞"
|
||||
|
||||
if (minutes > 0) {
|
||||
remainingMinutes = minutes
|
||||
updateUI(status, "Runs for: $remainingMinutes min", Color.GREEN)
|
||||
countdownRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
remainingMinutes--
|
||||
if (remainingMinutes > 0) {
|
||||
status.text = "Läuft: ${remainingMinutes} min"
|
||||
updateUI(status, "Runs for: $remainingMinutes min", Color.GREEN)
|
||||
handler.postDelayed(this, 60_000L)
|
||||
} else {
|
||||
stopSound(btn, status)
|
||||
stopSound(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
handler.postDelayed(countdownRunnable!!, 60_000L)
|
||||
} else {
|
||||
updateUI(status, "Runs: ∞", Color.GREEN)
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSound(btn: Button, status: TextView) {
|
||||
private fun stopSound(status: TextView) {
|
||||
if (isPlaying) {
|
||||
soundPool.stop(streamId)
|
||||
isPlaying = false
|
||||
}
|
||||
remainingMinutes = 0
|
||||
handler.removeCallbacks(countdownRunnable ?: Runnable { })
|
||||
status.text = "Gestoppt"
|
||||
status.setBackgroundColor(Color.RED)
|
||||
status.setTextColor(Color.BLACK)
|
||||
updateUI(status, "Stopped", Color.RED)
|
||||
}
|
||||
|
||||
btn.text = "Start"
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
private fun updateUI(view: TextView, text: String, color: Int) {
|
||||
view.text = text
|
||||
view.setBackgroundColor(color)
|
||||
view.setTextColor(Color.BLACK)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in New Issue
Block a user