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 播放提示音的更多相关文章

  1. android开发(44) 使用了 SoundPool 播放提示音

    SoundPool 一个声音播放的辅助类,从名字可以看出,它具有 “池”的能力,它先加载声音文件到内存,以支持多次播放声音文件. 特点 SoundPool适合 短小的 声音文件 SoundPool适合 ...

  2. 关于微信内置浏览器在ios上播放提示音的经验分享

    document.addEventListener("WeixinJSBridgeReady", function () { window.audio= new Audio() w ...

  3. Android笔记: 播放提示音 的简单方法

    public static void sendSound(Context mContext) { //上下文 Uri mUri= RingtoneManager.getDefaultUri(Ringt ...

  4. Android 极光推送JPush---自定义提示音

    极光推送提供三种方法实现Notification通知 三方开发平台发送普通消息,客户端设置PushNotificationBuilder,实现基础的Notification通知 三方开放平台发送普通消 ...

  5. Windows Phone 如何在程序中播放提示声音?

    在Windows Phone 中播放提示音可以使用 Microsoft.Xna.Framework.Audio 命名空间下的 SoundEffect 类.具体使用方法如下: 1. 根据声音文件路径创建 ...

  6. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

  7. iOS 提示音播放

    首先找到对应的素材 音频文件 写一个类继承 NSObject 命名为AudioUtil 导入支撑文件 #import <AVFoundation/AVFoundation.h> #impo ...

  8. Android 使用SoundPool播放音效

    在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...

  9. Android MediaPlayer播放一般音频与SoundPool播放短促的音效

    [1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...

随机推荐

  1. 3.4.2 Undefined类型【JavaScript高级程序设计第三版】

    Undefined 类型只有一个值,即特殊的 undefined.在使用 var 声明变量但未对其加以初始化时,这个变量的值就是 undefined,例如: var message; alert(me ...

  2. MongoDb第一天

    安装之后进入cmd.进入到安装目录下的bin目录下. 任意选一个空目录,建立db,log的文件夹.之后终端命令行里面输入回车. D:\ProgramFiles\MongoDB\Server\3.6\b ...

  3. cf978E Bus Video System

    The busses in Berland are equipped with a video surveillance system. The system records information ...

  4. Matplotlib库介绍

    pyplot的plot()函数 pyplot的中文显示 pyplot的文本显示 pyplot的子绘图区域

  5. 4,MongoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MongoDB

    MongoDB中的关键字有很多, $lt $gt $lte $gte 等等,这么多我们也不方便记,这里我们说说几个比较常见的 一.查询中常见的 等于 大于 小于 大于等于 小于等于 等于 : 在Mon ...

  6. SpringMVC---springMVC配置文件(springweb.xml)简介

    再web.xml中设置HTTP请求的中央调度处理器DispatcherServlet时,会指定SpringMVC配置文件,这里取名springweb.xml是因设置DispatcherServlet时 ...

  7. equals和toString

    Object的equals方法默认比较地址值.所以当需要比较两个对象的内容时需要重写equals方法.

  8. Python运算符及逻辑运算

    基本运算符 运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运 ...

  9. (转)Unreal Shader模块(四): 着色器编译

    本文为(转):Unreal 调试着色器编译过程     调试着色器编译过程 Rolando Caloca 在 April 19, 2016 | 学习编程 Share on Facebook  Shar ...

  10. sources.list

    deb http://debian.ustc.edu.cn/ubuntu/ precise main multiverse restricted universe deb http://debian. ...