SoundPool音频播放短促的声音使用和封装

1、初始化SoundPool

maxStreams : 最大的流数量

streamType : 流的类型 AudioManager.STREAM_SYSTEM

srcQuality : 音频质量  默认 0 

soundPool=new SoundPool(int maxStreams , int streamType , int srcQuality)

2、加载音频 :一般把音频文件放置在res 的raw文件下


context : 上下文

resId : R.raw. beep 

priority : 优先级  设置为1即可

使用加载音频   

soundPool.load(Context context , int resId , int priority);

3、播放音频 : 

soundId : 声音的id  就是2中load 音频的顺序

leftVolume : 左声道音量控制 0.0 -- 1.0

rightVolume : 右声道音量控制 0.0 -- 1.0

priority : 优先级 0最低

loop : 0不循环  1循环

rate : 1 正常播放

soundPool.play(int soundId , float leftVolume , float rightVolume , int priority , int loop , float rate);


4、小结

//初始化SoundPool
SoundPool soundPool = new SoundPool(10 , AudioManager.STREAM_SYSTEM, 5);
//加载deep音频文件
soundPool(this , R.raw.been);
//播放音频
soundPool.play(1,1, 1, 0, 0, 1);

5、封装 SoundPoolUtils

public class SoundPoolUtils
{
    //SoundPool 对象
    public static SoundPool mSoundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
    public static SoundPoolUtils soundPoolUtils;
    // 上下文
    static Context mContext;

    /**
     * 初始化 SoundPool
     *
     * @param context 上下文
     *
     * @return
     */
    public static SoundPoolUtils init(Context context)
    {
        if (soundPoolUtils == null) {
            soundPoolUtils = new SoundPoolUtils();
        }
        // 初始化声音
        mContext = context;

        mSoundPool.load(mContext, R.raw.beep, 1);
        mSoundPool.load(mContext, R.raw.beep2, 2);

        return soundPoolUtils;
    }

    /**
     * 播放声音
     *
     * @param soundId load 进去的声音编号
     */
    public static void play(int soundId)
    {
        mSoundPool.play(soundId, 1, 1, 0, 0, 1);
    }

}


转载:http://blog.csdn.net/yy1300326388/article/details/47044869

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值