问题:
DDMS报的错是sample not ready的问题,也就是说是在load加载音乐文件出错,导致在play播放音乐时显示not ready; 在SoundPool中有setOnLoadCompleteListener方法用来判断音乐加载是否完成。
解决方法:
调用setOnLoadCompleteListener方法来确保音乐加载完成,注意需要SoundPool.OnLoadCompleteListener listener需要实现SoundPool.OnLoadCompleteListener接口。
SoundPool soundPool = new SoundPool(MAX_SOUND_NUMBERS, AudioManager.STREAM_MUSIC, 1);
soundPool.load(context, R.raw.alarmgps, 1);
soundPool.load(context, R.raw.alarmflux, 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i2) {
soundPool.play(1, //声音id
1, //左声道
1, //右声道
1, //优先级
0, // 0表示不循环,-1表示循环播放
1);//播放比率,0.5~2,一般为1
}
});