SoundPool详解

SoundPool工具类

public class SoundPoolUtil {
    private static SoundPool soundpool;
    private static SoundPoolUtil instance;
    private static Context context;

    public static SoundPoolUtil getInstance(Context mcontext) {
        if (instance == null) {
            instance = new SoundPoolUtil();
        }
        context = mcontext;
        if (Build.VERSION.SDK_INT > 21) {
            SoundPool.Builder builder = new SoundPool.Builder();
            //传入音频数量
            builder.setMaxStreams(20);
            //AudioAttributes是一个封装音频各种属性的方法
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            //设置音频流的合适的属性
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
            //加载一个AudioAttributes
            builder.setAudioAttributes(attrBuilder.build());
            soundpool = builder.build();
        } else {
//        第一个参数为soundPool可以支持的声音数量,这决定了Android为其开设多大的缓冲区,第二个参数为声音类型,
//        在这里标识为系统声音,除此之外还有AudioManager.STREAM_RING以及AudioManager.STREAM_MUSIC等
//        系统会根据不同的声音为其标志不同的优先级和缓冲区,最后参数为声音品质,品质越高,声音效果越好,但耗费更多的系统资源
            soundpool = new SoundPool(20, AudioManager.STREAM_SYSTEM, 5);
        }
        //第二个参数资源文件,第三个参数为声音的优先级,当多个声音冲突而无法同时播放时,系统会优先播放优先级高的。
        soundpool.load(context, R.raw.closed, 1);
        soundpool.load(context, R.raw.closing, 1);
        soundpool.load(context, R.raw.opened, 1);
        soundpool.load(context, R.raw.opening, 1);
        soundpool.load(context, R.raw.opening2, 1);
        soundpool.load(context, R.raw.stop, 1);
        soundpool.load(context, R.raw.doerror, 1);
        soundpool.load(context, R.raw.beep, 1);
        soundpool.load(context, R.raw.bindcamera, 1);
        soundpool.load(context, R.raw.id, 1);
        soundpool.load(context, R.raw.login, 1);
        soundpool.load(context, R.raw.logined, 1);
        soundpool.load(context, R.raw.loginfail, 1);
        soundpool.load(context, R.raw.offline, 1);
        soundpool.load(context, R.raw.paizhao, 1);
        soundpool.load(context, R.raw.timeout, 1);
        soundpool.load(context, R.raw.wifi, 1);
//监听加载资源完成的回调
soundpool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

    }
});
       
        return instance;
    }


    /*
     播放,你想要播放第几个就传入几
play(资源号上面设置的顺序, 左声道音量 ,右声道音量,优先级,循环播放次数-1为一直循环,回放速度:该值在0.5-2.0之间 1为正常速度)
      */
    public void play(int pid) {
        soundpool.play(pid, 1, 1, 0, 0, 1);
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值