版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010838785/article/details/88987777
/**
* 作者:guoyzh
* 时间:2019/3/12 19:07
* 功能:加载音频文件工具类
*/
public class SoundPoolUtils {
private Context context;
private static SoundPool soundPool;
private static int soundID;
/**
* 播放音频文件
*
* @param context
* @param resId 音频文件 R.raw.xxx
* @param repeatTime 重复次数
*/
public static void play(Context context, int resId, int repeatTime) {
if (soundPool == null) {
// 版本兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder().setMaxStreams(10).build();
} else {
//第一个参数是可以支持的声音数量,第二个是声音类型,第三个是声音品质
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
}
}
soundID = soundPool.load(context, resId, 1);
// 该方法防止sample not ready错误
soundPool.setOnLoadCompleteListener((soundPool, i, i2) -> {
soundPool.play(
soundID, //声音id
1, //左声道
1, //右声道
1, //播放优先级【0表示最低优先级】
repeatTime, //循环模式【0表示循环一次,-1表示一直循环,其他表示数字+1表示当前数字对应的循环次数】
1);//播放速度【1是正常,范围从0~2】一般为1
});
}
}