[英]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 :我需要在
Kotlin
使用soundPool
,我编写了如下代码:
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.声音仅在我第一次调用时播放,然后我必须清除模拟器数据或在重新安装应用程序之前关闭并重新打开我的三星 A50。
The boo.mp3 file is 213Ko What I am missing? boo.mp3 文件是 213Ko 我缺少什么? any idea most welcome.
任何想法最受欢迎。
In fact, the soundPool need some time to load the sound.实际上,soundPool 需要一些时间来加载声音。 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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.