简体   繁体   中英

SoundPool plays sound only the first time, when it is called

I need to use soundPool in Kotlin and I wrote my code like below :

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.

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()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM