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. 17-比赛1 A - Weak in the Middle (栈)

    题目描述 给定长度为 N 的序列 A.每天,序列 A 中所有比两侧元素都小的元素都会消失.对于原序列中所有元素,请求出它会在第几天之后消失(天数从 1 开始计算),或者指出它不会消失. 数据范围 1 ...

  2. 【UE4】二十三、UE4笔试面试题

    在CSDN博客看到的,带着这些问题,多多留意,正所谓带着问题学习. 一. 1.Actor的EndPlay事件在哪些时候会调用? 2.BlueprintImplementableEvent和Bluepr ...

  3. Android面试收集录 Android入门

    1.Android的特点有哪些? 编程语言是Java或Kotlin,Android中的Java字节码是允许在Dalvik虚拟机上的 支持4大组件 Android内置了WebKit核心的浏览器,支持H5 ...

  4. PHP.29-TP框架商城应用实例-后台6-商品会员添加-价格、级别

    首先把需求分析搞清楚 主要实现两个功能 1.会员管理,设置成为会员的要求 2.添加商品时,可设置会员优惠价格 具体实现 1.建表[会员级别限定表p39_member_level{Id,级别名称,积分下 ...

  5. Linux 安装github并配置ssh

    首先,你得有个github帐号. 1.用apt-get install git的方式安装git test@er:/$ sudo add-apt-repository ppa:git-core/ppa ...

  6. 6.定制10MINS首页1

    原始代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  7. spring里面的context:component-scan

    原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...

  8. 图的深度优先遍历&广度优先遍历

    1.什么是图的搜索? 指从一个指定顶点可以到达哪些顶点   2.无向完全图和有向完全图 将具有n(n-1)/2条边的无向图称为无向完全图(完全图就是任意两个顶点都存在边). 将具有n(n-1)条边的有 ...

  9. Druid数据库连接池及内置监控的配置和使用

    Druid介绍 Druid首先是一个数据库连接池,并且是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss Da ...

  10. laravel5.5http会话机制

    1.配置文件 config/session.php 大多数是用file驱动,将session保存在storage/framework/sessions,可以考虑使用redis或者memcached 驱 ...