在使用SoundPool播放声音文件的时候,我想点击一个按钮播放声音,然后在点击时暂停播放,在点击在播放,但是每次都是第一次是好的,第二次在点击想要播放的时候就会没有声音,我一开始用的是
if (isPlaying == false) {
// 播放报警声音
streamID = soundPool.play(1, 1, 1, 0, -1, 1);
isPlaying = true;
} else {
soundPool.stop(streamID);
isPlaying = false;
}
这种方法写的,后来了解到可以用soundPool.pause(streamID)来暂停播放的声音,然后在soundPool.resume(streamID);就可以了。代码如下:
if (isPlaying == false) {
// 播放报警声音
if (streamID == 0) {
streamID = soundPool.play(1, 1, 1, 0, -1, 1);
isPlaying = true;
} else {
soundPool.resume(streamID);
isPlaying = true;
}
} else {
soundPool.pause(streamID);
isPlaying = false;
}
还有一点在ondestroy的时候要记得释放soundPool, 即soundPool.release();否则在进入可能会出现点击播放没有声音的问题。