之前做了一个需求是在接收到推送时给一个自定义的提示音,比较了一下MediaPlayer、SoundPool等几种播放方式,最终选择了SoundPool。
但是
遇到了坑,在此记录一下,12s的音频在大概5秒还是6秒的时候就停止播放了
代码如下:
private void pool() {
SoundPool soundPool;
//实例化SoundPool
//sdk版本21是SoundPool 的一个分水岭
if (Build.VERSION.SDK_INT >= 21) { //大于等于安卓5.0的
SoundPool.Builder builder = new SoundPool.Builder();
//传入最多播放音频数量,
builder.setMaxStreams(3);
//AudioAttributes是一个封装音频各种属性的方法
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
//设置音频流的合适的属性
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
//加载一个AudioAttributes
builder.setAudioAttributes(attrBuilder.build());
soundPool = builder.build();
} else {
/**
* 第一个参数:int maxStreams:SoundPool对象的最大并发流数
* 第二个参数:int streamType:A