I try to play a short .mp3 sound (beep) continuously, 100 times a minute. It plays around 30-45 times correctly (more on newer devices), then it stops for some time (~30 seconds), and then plays again (~10 times only).
I tried both SoundPool
(playBeepSound1
) and MediaPlayer
(playBeepSound2
) so maybe I'm using Handler
wrong.
private val delay: Float = 60.0F / 100.0F * 1000.0F // 100 times per minute
private var handler: Handler? = null
private var soundPool: SoundPool? = null
private var mediaPlayer: MediaPlayer? = null
override fun onResume() {
super.onResume()
startBeeping()
}
private fun startBeeping() {
handler = Handler()
handler?.postDelayed(object : Runnable {
override fun run() {
handler?.postDelayed(this, delay.toLong())
playBeepSound1() // SoundPool
// playBeepSound2() // MediaPlayer
}
}, delay.toLong())
}
private fun playBeepSound1() {
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
soundPool = SoundPool.Builder()
.setAudioAttributes(attributes)
.build()
soundPool?.setOnLoadCompleteListener { soundPool, soundId, _ ->
soundPool.play(soundId, 1.0f, 1.0f, 0, 0, 1.0f);
}
soundPool?.load(baseContext, R.raw.beep_sound, 1)
}
private fun playBeepSound2() {
mediaPlayer = MediaPlayer.create(this, R.raw.beep_sound)
mediaPlayer?.start()
}
override fun onPause() {
super.onPause()
stopBeeping()
}
private fun stopBeeping() {
handler?.removeCallbacks(this)
handler = null
soundPool?.release()
soundPool = null
mediaPlayer?.release()
mediaPlayer = null
}
Any ideas about how can I play a short sound in a loop?
Update:
I tested it on different devices with different OS version (Android 6, Android 9, also emulators) and the result is almost the same. The only difference is the time after it stops beeping.
The mp3 file has a size of 5KB.