Skip to content

Commit

Permalink
feat: wait on sound being ready before attempting to play
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 committed Sep 17, 2024
1 parent d3cfa7f commit e56b896
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TimeUpdateWorker(
private lateinit var tts: TextToSpeech
private lateinit var soundPool: SoundPool
private var tickSoundId: Int = 0
private var isSoundLoaded: Boolean = false

override fun doWork(): Result {
val ticksEnabled = inputData.getBoolean("ticks_enabled", true)
Expand All @@ -31,7 +32,10 @@ class TimeUpdateWorker(
if (speechEnabled && minute % 15 == 0) {
speakTime(calendar)
} else if (ticksEnabled) {
playTickSound()
// Only play the sound if it's loaded
if (isSoundLoaded) {
playTickSound()
}
}
}

Expand All @@ -49,6 +53,12 @@ class TimeUpdateWorker(
.setAudioAttributes(attributes)
.build()

soundPool.setOnLoadCompleteListener { _, _, status ->
if (status == 0) {
isSoundLoaded = true
}
}

tickSoundId = soundPool.load(applicationContext, R.raw.tick_sound, 1)
}

Expand Down

0 comments on commit e56b896

Please sign in to comment.