声池 |检查流是否正在播放



我正在使用 soundpool 播放短音频文件。单击按钮后,它应该播放音频,第二次它应该暂停流。我使用了这样的函数detectPlayPause(sound2, activity!!.applicationContext)问题是它没有暂停,而是再次以两个流的形式播放该声音

fun detectPlayPause(sound: Int, context: Context) {
val audioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager
if (audioManager.isStreamMute(sound)) {
soundPool.play(sound, 1F, 1F, 0, -1, 1F)
} else {
soundPool.pause(sound)
}}

**

您误用了audioManager.isStreamMute()。该函数采用AudioManager常量,如STREAM_MUSIC。这与从SoundPool.load()返回的声音ID是不同的。

isStreamMute()不会告诉您声音是否在播放。这更多用于检测设备的静音设置;用户是否选择了静音/振动/静音等。

相反,只需使用布尔变量跟踪您的播放状态。

if (!playing) {
playing = true
soundPool.play(sound, 1F, 1F, 0, -1, 1F)
} else {
playing = false
soundPool.pause(sound)
}

最新更新