美文网首页
SoundPool-简短音频播放的简单封装

SoundPool-简短音频播放的简单封装

作者: FrankDaddy | 来源:发表于2017-01-20 09:46 被阅读355次

SoundPool一般都用来播放很简短的声音提示,源声音文件放在APK里面的,下面做一些简单的封装
思路:弄成单例模式,取全局的上下文初始化,预先加载声音,要用的位置直接调用就可以
直接上代码

/**
 * Created by Frank on 2017/1/11.
 * 声音文件-->用于播放提示语
 */
public class SoundPoolUtils {
    int streamVolume;// 音效的音量
    private SoundPool soundPool;// 定义SoundPool 对象
    private HashMap<Integer, Integer> soundPoolMap;// 定义HASH表
    //获取单例
    public static SoundPoolUtils getInstance(){
        return SoundPoolUtils.SingletonHolder.INSTANCE;
    }
    //在访问SoundPoolUtils时创建单例
    private static class SingletonHolder{
        private static final SoundPoolUtils INSTANCE = new SoundPoolUtils();
    }
    // 构造方法私有
    private SoundPoolUtils() {
        if (soundPool == null){
            // 初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
            soundPool = new SoundPool(100, AudioManager.STREAM_MUSIC, 100);
            // 初始化HASH表
            soundPoolMap = new HashMap<Integer, Integer>();
            // 获得声音设备和设备音量
            AudioManager mgr = (AudioManager) MyApplication.getContext().getSystemService(Context.AUDIO_SERVICE);
            streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
            loadallSfx();
        }
    }
    public void loadSfx(int raw, int ID) {
        // 把资源中的音效加载到指定的ID(播放的时候就对应到这个ID播放就行了),MyApplication是全局上下文
        soundPoolMap.put(ID, soundPool.load(MyApplication.getContext(), raw, ID));
    }
    /**
     * play(1, 0); // 默认,警告提示音
     */
    public void play(int sound, int uLoop) {
        soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, uLoop, 1f);
    }
    public void loadallSfx(){
        // 先做特殊判断
        String where = "四川";
        if(where.equals("四川")){
            // 加载默认的声音
            loadSfx(R.raw.sound_default_1, 1);

        }else if(where.equals("河北")){
            // 其他地方话
            loadSfx(R.raw.sound_default_1, 1);
        }
    }
}

相关文章

网友评论

      本文标题:SoundPool-简短音频播放的简单封装

      本文链接:https://www.haomeiwen.com/subject/trewbttx.html