Android开发之使用SoundPool加载声音文件

  

在使用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();否则在进入可能会出现点击播放没有声音的问题。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值