SoundPool 播放提示音
SoundPool
一个声音播放的辅助类,从名字可以看出,它具有 “池”的能力,它先加载声音文件到内存,以支持多次播放声音文件。
特点
- SoundPool适合 短小的 声音文件
- SoundPool适合播放 “需要多次播放的提示音”,比如在 一些常用的 请登录,请点击什么的
- 相比mediaPlayer,耗用资源更少
- 支持 同时 播放多个声音
使用方法
创建实例
mSoundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
soundPoolMap = new HashMap<Integer, Integer>(); //这里我创建一个 hash 表,用于记录加载过的声音的ID,一般我们会定义一个常量作为检索该声音的KEY
加载声音文件
soundPoolMap.put(KEY_SOUND_A1, mSoundPool.load(this, R.raw.a1, 1));
soundPoolMap.put(KEY_SOUND_A2, mSoundPool.load(this, R.raw.a2, 1));//注意,这里 hash表里 记录
播放声音文件
mSoundPool.play(soundPoolMap.get(KEY_SOUND_A1), 1, 1, 0, 0, 1); //注意,这里从hash表里取出了具体的ID
注册一个监听器,在加载声音完毕的时候获得消息
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
alert(" " + sampleId);
}
});
应用
public class AlarmSoundUtil {
public static final int KEY_input_your_number = 101;
public static final int KEY_selectgekou = 103;
public static final int KEY_selectchongzhi = 104;
public static final int KEY_please_input_pickup_password = 105;
public static final int KEY_sao = 107;
public static final int KEY_closebox = 108;
public static final int KEY_please_input_consignee_phone = 109;
public static final int KEY_open_box_error = 110;
public static final int KEY_deliver_closebox = 111;
public static final int KEY_finishe = 112;
public static final int KEY_CHONGZHI_JIN_E = 113;
public static final int KEY_TOU_BI = 114;
public static final int KEY_TOU_BI_2 = 115;
public static final int KEY_DU = 118;
//请输入取件密码
public static final int KEY_please_input_waybill = 116;
//请输入手机号码和动态密码
public static final int KEY_please_input_phone_and_dynamicpassword = 117; /**
* 数字语音的开始位置:后续位置即 KEY_NUMBER_START + n ,n = 0,1,2,3...
*/
public static final int KEY_NUMBER_0 = 201509150;
public static final int KEY_NUMBER_1 = 201509151;
public static final int KEY_NUMBER_2 = 201509152;
public static final int KEY_NUMBER_3 = 201509153;
public static final int KEY_NUMBER_4 = 201509154;
public static final int KEY_NUMBER_5 = 201509155;
public static final int KEY_NUMBER_6 = 201509156;
public static final int KEY_NUMBER_7 = 201509157;
public static final int KEY_NUMBER_8 = 201509158;
public static final int KEY_NUMBER_9 = 201509159; private static AlarmSoundUtil mInstance;
private SoundPool soundPool;
private SparseIntArray soundMap; public static AlarmSoundUtil getInstance() {
if (mInstance == null) {
mInstance = new AlarmSoundUtil(BoxApp.getApp());
}
return mInstance;
} private AlarmSoundUtil(Context context) {
soundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 5);
soundMap = new SparseIntArray();
load(context);
} private void load(Context context) {
if (Constants.DEBUG)
return;
soundMap.put(KEY_input_your_number, soundPool.load(context, R.raw.input_your_number, 1)); // 请输入你的手机号码及短信验证码
//soundMap.put(102, soundPool.load(context,
// R.raw.toudifinish, 1));// 投递已完成
soundMap.put(KEY_selectgekou, soundPool.load(context, R.raw.selectgekou, 1));// 请选择可用箱体
soundMap.put(KEY_selectchongzhi, soundPool.load(context, R.raw.selectchongzhi, 1));// 请选择充值方式
soundMap.put(KEY_please_input_pickup_password, soundPool.load(context, R.raw.please_input_pickup_password, 1));// 请输入取件密码
//soundMap.put(106, soundPool.load(context,
// R.raw.qujianfinish, 1));//取件已完成
soundMap.put(KEY_sao, soundPool.load(context, R.raw.sao, 1));// 投递,请扫描单号并输入收件人手机号码
soundMap.put(KEY_closebox, soundPool.load(context, R.raw.closebox, 1));// 请关闭箱门
soundMap.put(KEY_please_input_consignee_phone, soundPool.load(context, R.raw.please_input_consignee_phone, 1));// 寄存,请输入收件人手机号码
soundMap.put(KEY_open_box_error, soundPool.load(context, R.raw.open_box_error, 1));// 打开箱体失败
soundMap.put(KEY_deliver_closebox, soundPool.load(context, R.raw.deliver_closebox, 1));// //请投递完成后关闭相关
soundMap.put(KEY_finishe, soundPool.load(context, R.raw.finishe, 1));// //箱门已开,取件后关闭箱门
soundMap.put(KEY_CHONGZHI_JIN_E, soundPool.load(context, R.raw.czje, 1));// //充值金额
soundMap.put(KEY_TOU_BI, soundPool.load(context, R.raw.tb, 1));// //投币
soundMap.put(KEY_TOU_BI_2, soundPool.load(context, R.raw.tb, 1));// //箱门已开,请投递,如果箱门未开,请取消投递
soundMap.put(KEY_please_input_waybill, soundPool.load(context, R.raw.please_input_waybill, 1));
soundMap.put(KEY_please_input_phone_and_dynamicpassword, soundPool.load(context, R.raw.please_input_phone_and_dynamicpassword, 1));
soundMap.put(KEY_DU, soundPool.load(context, R.raw.du, 1)); soundMap.put(KEY_NUMBER_0, soundPool.load(context, R.raw.number_0, 1));
soundMap.put(KEY_NUMBER_1, soundPool.load(context, R.raw.number_1, 1));
soundMap.put(KEY_NUMBER_2, soundPool.load(context, R.raw.number_2, 1));
soundMap.put(KEY_NUMBER_3, soundPool.load(context, R.raw.number_3, 1));
soundMap.put(KEY_NUMBER_4, soundPool.load(context, R.raw.number_4, 1));
soundMap.put(KEY_NUMBER_5, soundPool.load(context, R.raw.number_5, 1));
soundMap.put(KEY_NUMBER_6, soundPool.load(context, R.raw.number_6, 1));
soundMap.put(KEY_NUMBER_7, soundPool.load(context, R.raw.number_7, 1));
soundMap.put(KEY_NUMBER_8, soundPool.load(context, R.raw.number_8, 1));
soundMap.put(KEY_NUMBER_9, soundPool.load(context, R.raw.number_9, 1));
} /**
* 音频资源ID 播放语音
*
* @param key 音频资源ID
*/
public void play(int key) {
if (soundPool == null) return;
if (Constants.DEBUG)
return;
soundPool.play(soundMap.get(key), 1, 1, 0, 0, 1);
} public void release() {
if (mSoundPool != null) {
mSoundPool.release();
}
} }
SoundPool 播放提示音的更多相关文章
- android开发(44) 使用了 SoundPool 播放提示音
SoundPool 一个声音播放的辅助类,从名字可以看出,它具有 “池”的能力,它先加载声音文件到内存,以支持多次播放声音文件. 特点 SoundPool适合 短小的 声音文件 SoundPool适合 ...
- 关于微信内置浏览器在ios上播放提示音的经验分享
document.addEventListener("WeixinJSBridgeReady", function () { window.audio= new Audio() w ...
- Android笔记: 播放提示音 的简单方法
public static void sendSound(Context mContext) { //上下文 Uri mUri= RingtoneManager.getDefaultUri(Ringt ...
- Android 极光推送JPush---自定义提示音
极光推送提供三种方法实现Notification通知 三方开发平台发送普通消息,客户端设置PushNotificationBuilder,实现基础的Notification通知 三方开放平台发送普通消 ...
- Windows Phone 如何在程序中播放提示声音?
在Windows Phone 中播放提示音可以使用 Microsoft.Xna.Framework.Audio 命名空间下的 SoundEffect 类.具体使用方法如下: 1. 根据声音文件路径创建 ...
- ios开发小技巧之提示音播放与震动
在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...
- iOS 提示音播放
首先找到对应的素材 音频文件 写一个类继承 NSObject 命名为AudioUtil 导入支撑文件 #import <AVFoundation/AVFoundation.h> #impo ...
- Android 使用SoundPool播放音效
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...
- Android MediaPlayer播放一般音频与SoundPool播放短促的音效
[1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...
随机推荐
- pyhton——logging日志模块的学习
https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模 ...
- 【Hadoop/Hive/mapreduce】系列之如何删除HIVE 表格的分区
今天的一个业务场景就是要把三年的数据从第一天不停的融合起来,每一天作为表格一个新的分区.由于空间有限,数据量很大,可能每天数据都是几十个G的大小.所以我需要做的一点就是在融合这一天之后,删除一天的分区 ...
- python系列4之装饰器
目录 递归算法解析 冒泡排序解析 装饰器解析 一. 递归 1. 递归的定义 递归(Recursion),又成为递回,在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.递归一词还较长用于描述以 ...
- 开放定址法——平方探测(Quadratic Probing)
为了消除一次聚集,我们使用一种新的方法:平方探测法.顾名思义就是冲突函数F(i)是二次函数的探测方法.通常会选择f(i)=i2.和上次一样,把{89,18,49,58,69}插入到一个散列表中,这次用 ...
- perl语言入门总结-第5章-输入输出
读取标准输入 chomp($line = <STDIN>); #去掉后面的换行 while (defined($line = <STDIN>)) { print "I ...
- 【文件处理】RandomAccessFile
一,RandomAccessFile的用途: 使用RandomAccessFile的最大好处在于,一般的InputStream和OutputStream类对于文件都是顺序读取的,不能跳跃读取数据.而R ...
- 2,版本控制git --分支
有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处理分支的方式可谓是难以置信的轻量, ...
- MySQL之查询性能优化(四)
优化特定类型的查询 COUNT()的作用 COUNT()是一个特殊函数,有两个非常不同的作用:它可以统计某个列值的数量,也可以统计行数.在统计列值时要求列值是非空的(不统计NULL). 如果在COUN ...
- wget 下载页面下所有文件
先介绍几个参数:-c 断点续传(备注:使用断点续传要求服务器支持断点续传),-r 递归下载(目录下的所有文件,包括子目录),-np 递归下载不搜索上层目录,-k 把绝对链接转为相对链接,这样下载之后的 ...
- Javascript进阶:对象实例属性和方法
Ecmascript中,Object类型是所有它的实例的基础.换句话说,Object类型所具有的任何属性和方法也同样存在于更具体的对象中. Object的每个实例都具有以下属性和方法,这些都能方便于我 ...