Android 短提示音播放SoundPool

xiaoxiao2025-08-02  27

/** * @作者:TJ * @时间:2018/10/16-11:12 * @描述:短提示音播放 */ public class SoundUtil { /** * 上下文 */ private Context mContext; /** * 添加的声音资源参数 */ private HashMap<Integer, Integer> soundPoolMap; /** * 声音池 */ private SoundPool mSoundPool; /** * 单例 */ private static SoundUtil instance; private SoundUtil(Context context) { mContext = context; } public static SoundUtil getInstance(Context context) { if (instance == null) { instance = new SoundUtil(context); instance.init(); } return instance; } /** * 初始化声音 */ public void init() { mSoundPool = new SoundPool.Builder().build(); soundPoolMap = new HashMap<>(); //刷卡滴 putSound(0, R.raw.beep); //消费撤销 putSound(1, R.raw.consume_success); putSound(2, R.raw.repeal); //计算器点 putSound(3, R.raw.tock); putSound(4, R.raw.tink); } private void putSound(int order, int soundRes) { // 上下文,声音资源id,优先级 soundPoolMap.put(order, mSoundPool.load(mContext, soundRes, 0)); } /** * 根据序号播放声音 * @param order */ public void playSound(int order) { mSoundPool.play( soundPoolMap.get(order), 1f, //左耳道音量【0~1】 1f, //右耳道音量【0~1】 0, //播放优先级【0表示最低优先级】 0, //循环模式【0表示循环一次,-1表示一直循环, 其他表示数字+1表示当前数字对应的循环次数】 1 //播放速度【1是正常,范围从0~2】 ); } /** * 释放内存 */ public void release() { LogUtil.e("SoundUtil", "release: "); if (mSoundPool != null) { mSoundPool.release(); mSoundPool = null; } instance = null; } }
转载请注明原文地址: https://www.6miu.com/read-5034127.html

最新回复(0)