SoundPool音频播放

    学之广在于不倦,不倦在于固志。 ——晋·葛洪­

   (学问的渊博在于学习时不知道厌倦,而学习不知厌倦在于有坚定的目标)

注意:已更新SoundPool在停止播放音效时的错误    更新时间:2018.8.27

001.Android中播放简短的音频建议首选SoundPool:

    ---> SoundPool使用音效池来管理多个简短的音效,并且以ID的形式管理音频文件,SoundPool还支持设置音频的品质、声音大小、播放比率、是否循环等

    ---> 用于播放短促、体积比较小的音频文件,如通知铃声、解锁铃声、相机照相声音

      ---> SoundPool在API21之前通过类构造器直接new即可使用,详细见下面代码

           在API21之后可以通过建造者模式创建SoundPool对象,可以设置多项内容,即

SoundPool mSoundPlayer= new SoundPool.Builder().setMaxStreams(8).build();

      ---> SoundPool可以播放.MP3等格式的音频(亲测这种有效)

           一般将音频文件存放于raw文件夹:在Android工程目录下的res目录下新建raw文件夹,将音频文件放入

          

     ---> 详细使用见代码:

public class SoundPoolUtils {
    /**
     * int maxStreams:最大的流的数量(播放数量)
     * int streamType:流的类型
     * int srcQuality:频的质量,默认是0,目前没有影响
     */
    private static SoundPool mSoundPlayer = new SoundPool(8, AudioManager.STREAM_SYSTEM, 0);
    private static SoundPoolUtils soundPlayUtils;
    // 上下文
    private static Context mContext;

    /**
     * 初始化
     *
     * @param context
     */
    public static SoundPoolUtils init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPoolUtils();
        }

        // 初始化声音
        mContext = context;

        /**
         * Context context,
         * int resId, 音频文件的地址:R.raw.deep
         * int priority,优先级:都是短促音频无影响设置为1即可
         */
        mSoundPlayer.load(mContext, R.raw.guanbitankuang, 1);// 1
        mSoundPlayer.load(mContext, R.raw.quweihuidaixunzedaanhou, 1);// 2
        mSoundPlayer.load(mContext, R.raw.zengjiajinbi, 1);// 3
        mSoundPlayer.load(mContext, R.raw.zhaoxiangji1, 1);// 4
        mSoundPlayer.load(mContext, R.raw.zhaoxiangji2, 1);// 5

        return soundPlayUtils;
    }

    /**
     * 播放声音
     * 注意,此处必须返回播放的流Id,因为在后面停止的时候需要传对应的id来停止
     */
    public static int play(int soundID) {
        /**
         * int soundID, 声音的id(即:load到SoundPool的顺序,从1开始)
         * float leftVolume, 左\右声道的音量控制, 0.0 到 1.0
         * float rightVolume, 左\右声道的音量控制, 0.0 到 1.0
         * int priority, 优先级,0是最低优先级
         * int loop, 是否循环播放,0为不循环,-1为循环
         * float rate 播放比率,从0.5到2,一般为1,表示正常播放
         */
        return mSoundPlayer.play(soundID, 1, 1, 0, 0, 1);
    }

    /**
     * 释放资源
     * 注意:此处如果你传的是你播放的对应的id就会出现第一次release正常停止,第二次停止不了,原因可以看源码解释
     */
    public static void releaseSound(int sounId) {
        if (null != mSoundPlayer) {
            mSoundPlayer.stop(sounId);
            mSoundPlayer.release();
            mSoundPlayer = null;
        }
    }
}

      注意:在load声音之后并不能立即播放,因为声音还没有加载完成,应该给其充足的时间让其加载完成,比如在onCreate()方法中让其加载,在需要的地方调用play()。

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SoundPoolUtils.init(this);
    //......
}
mSound.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            //播放第4个音频文件,需要停止的话就拿到播放时返回的流Id即可
                            //SoundPoolUtils.play(4);
                            soundId=SoundPoolUtils.play(4);
                        }
                    });
 /**
     * 销毁时释放你的音效文件,soundId是你播放时返回的流Id
     */
    @Override
    protected void onDestroy() {
        SoundPoolUtils.releaseSound(soundId);
        super.onDestroy();
    }

002.Android中播放音、视频可以选用MediaPlayer:

     ---> MediaPlayer可以播放比较长的音视频文件,有播放状态的回调

     ---> MediaPlayer启动延迟高,需要及时释放资源,有各种状态判断

 

003.Android中播放音频还可以选用AudioTrack:

     ---> AudioTrack可用于播放pcm格式的音频,不是很了解这个API,仅供参考!

 

Last :关于SoundPool的详细资料可以参考官网:--官网直通车--

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值