-1

I need to use soundPool in my app.

The problem is that the sound is only played the first time I launch the app on the emulator. Then I have to uninstall the app to test again.

The boo.mp3 file is 213Ko What I am missing? any idea most welcome.

private fun playThatSound(sound: String) {
    var soundPool: SoundPool? = null
    var soundId: Int = 0

    soundPool = SoundPool(6, AudioManager.STREAM_MUSIC, 0)

when(sound) {
    "boo" -> {
        soundPool.load(baseContext, R.raw.boo, 1)
        soundId = R.raw.boo
    }
}
    soundPool.play(soundId, 1F, 1F, 0, 0, 1F)
}

I call it like that:

playThatSound("boo")

The sound is only played the first time when I called it, then I have to clear the emulator data or turn my Samsung A50 OFF and ON again before reinstalling the app.

The boo.mp3 file is 213Ko What I am missing? any idea most welcome.

LastCard1440
  • 1
  • 1
  • 2

1 Answers1

0

In fact, the soundPool need some time to load the sound. So I needed to use

onLoadComplete(soundPool: SoundPool?, sampleId: Int, status: Int) {
        // let us know that a sound has been loaded by the SoundPool
        
Toast.makeText(this, "Sound $numSoundsLoaded Loaded",Toast.LENGTH_SHORT).show()
    }
LastCard1440
  • 1
  • 1
  • 2