android播放系统音效,Android用SoundPool播放音效
SoundPool可以用来播放一些较短的音效,如一些信息提醒之类的
先来说下会用到的参数
SoundPool(int maxStreams, int streamType, int srcQuality)的参数
maxStreams:指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量
streamType:声音类型,流类型可以分为STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING,STREAM_MUSIC 和 STREAM_ALARM四种类型。在AudioManager
定义
srcQuality:声音品质(采样率变换质量),当前无效果,用0作为默认
SoundPool在API 21(Android 5.0)之后就过时了,用SoundPool.Builder
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
soundID:Load()返回的声音ID号
leftVolume:左声道音量设置
rightVolume:右声道音量设置
priority:播放声音的优先级,数值越高,优先级越大。
loop:是否循环:-1表示无限循环,0表示不循环,其他值表示要重复播放的次数
rate:播放速率:播放速率的取值范围是0.5至2.0,1.0为原始播放速率,2.0的播放速率为原始速率的两位。0.5的播放速率为原始速率的一半。
release()方法释放所有SoundPool对象占据的内存和资源,也可以指定要释放的ID
需要编辑的文件如下
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/rawPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="raw"/>
android:id="@+id/assetsPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="assets"/>
android:id="@+id/btn_release"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="释放SoundPool"/>
MainActivity.java
packagecn.wuxiaocheng.soundpool;
importandroid.content.res.AssetManager;
importandroid.media.AudioAttributes;
importandroid.media.AudioManager;
importandroid.media.SoundPool;
importandroid.os.Build;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importjava.util.HashMap;
publicclassMainActivityextendsAppCompatActivityimplementsOnClickListener {
privateButton btn_play1;
privateButton btn_play2;
privateButton btn_release;
privateAssetManager aManager;
privateSoundPool mSoundPool =null;
privateHashMap soundID =newHashMap();
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aManager = getAssets();
try{
initSP();
} catch(Exception e) {
e.printStackTrace();
}
bindViews();
}
privatevoidbindViews() {
btn_play1 = (Button) findViewById(R.id.rawPlay);
btn_play2 = (Button) findViewById(R.id.assetsPlay);
btn_release = (Button) findViewById(R.id.btn_release);
btn_play1.setOnClickListener(this);
btn_play2.setOnClickListener(this);
btn_release.setOnClickListener(this);
}
privatevoidinitSP()throwsException {
//当前系统的SDK版本大于等于21(Android 5.0)时
if(Build.VERSION.SDK_INT >=21) {
SoundPool.Builder builder = newSoundPool.Builder();
//传入音频数量
builder.setMaxStreams(2);
//AudioAttributes是一个封装音频各种属性的方法
AudioAttributes.Builder attrBuilder = newAudioAttributes.Builder();
//设置音频流的合适的属性
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
//加载一个AudioAttributes
builder.setAudioAttributes(attrBuilder.build());
mSoundPool = builder.build();
}
//当系统的SDK版本小于21时
else{//设置最多可容纳2个音频流,音频的品质为5
mSoundPool = newSoundPool(2, AudioManager.STREAM_SYSTEM,5);
}
soundID.put(1, mSoundPool.load(this, R.raw.raw,1));
soundID.put(2, mSoundPool.load(getAssets().openFd("assets.mp3"),1));//需要捕获IO异常
}
@Override
publicvoidonClick(View v) {
switch(v.getId()) {
caseR.id.rawPlay:
mSoundPool.play(soundID.get(1),1,1,0,0,1);
break;
caseR.id.assetsPlay:
mSoundPool.play(soundID.get(2),1,1,0,0,1);
break;
caseR.id.btn_release:
//回收SoundPool资源
mSoundPool.release();
break;
}
}
}
android播放系统音效,Android用SoundPool播放音效相关推荐
- 【Android -- 音效】使用 SoundPool 播放音效
前言 SoundPool 一般用来 播放密集,急促而又短暂的音效,比如特技音效:Duang~,游戏用得较多,你也可以为你的 APP 添加上这个音效,比如酷狗音乐进去的时候播放"哈喽,酷狗&q ...
- win7 android双系统,Win7+Android双系统 Acer AOD255评测
Win7+Android双系统 Acer AOD255评测 2010年09月16日 00:08作者:尹航编辑:尹航文章出处:泡泡网原创 分享 泡泡网上网本频道9月16日 Acer Aspire One ...
- android改变系统语言,Android 9.0设置系统语言
1.系统权限 android:sharedUserId="android.uid.system" 2.签名 通过手动签名的方式,用系统的签名文件为程序签名解决签名的问题,将程序加入 ...
- android 设置系统壁纸,Android HttpURLConnection下载网络图片设置系统壁纸
需求: 壁纸是url链接,get就能请求到,所以就用get请求到图片,把图片转化为bitmap,然后设置壁纸. 代码: 这里我封装了工具类 package xxxxx.utils; import an ...
- android:一卡通系统,基于Android嵌入式平台的QR码识别系统 - 中国一卡通网
图5( a) 为倾斜的条码,图5( b) 为采用双线性插值旋转的结果,实验表明,双线性插值处理后得到的图像具有良好的效果,有利于条码识别. 3. 4 图像的矫正 由于在拍摄时,摄像头的角度不会完全垂直 ...
- 华为 android p系统,基于Android P打造 华为EMUI 9.0发布:流畅度提升12.9%
9月1日晚间,华为在德国柏林国际电子消费展览会(IFA)上举行媒体沟通会,正式发布华为EMUI 9.0系统. 全新的EMUI 9.0系统基于Android P打造,官方介绍该系统流畅度提升12.9%, ...
- android修改系统density,Android应用开发Android 修改系统默认density
本文将带你了解Android应用开发Android 修改系统默认density,希望本文对大家学Android有所帮助. 如你所知在Anroid N 中,系统添加了多个级别的密度值供用户选择. 系统的 ...
- android 底层系统开发,Android底层
概述-Android系统架构 Android系统架构 概述-Application Application Android平台不仅仅是操作系统,同时也包含了许多应用,如桌面,浏览器,视频客户端,摄像头 ...
- Android查看系统事件,Android系统的事件处理概述
通过对Android基本组件的学习,也有接触少部分Android的事件处理,比如按钮的点击事件.选框的状态切换事件. 一.Android事件处理 Android提供了两种方式的事件处理:基于回调的事件 ...
- android mtk 系统架构,Android振动器(Vibrator)系统详解
平台 MTK6573 Android 振动器系统架构 驱动层 硬件抽象层 JNI框架层 Java应用层 一.驱动层 Android修改.新增Linux内核文件 /kernel/drivers/stag ...
最新文章
- Python3.5源码分析-垃圾回收机制
- MySQL 性能优化技巧
- oracle选择语言设置,oracle本地语言变量设置
- 树莓派python编程案例-树莓派Python编程指南.pdf
- 自然语言处理NLP-100例 | 第二篇:在线课程评论情感分析-本科毕设实战案例
- swift 函数.和匿名函数
- boost::hana::then用法的测试程序
- CentOS 6.0安装ipvsadm 1.26错误笔录
- 输出倒逼输入 加速自己成长
- hdc mfc 画扇形图_MFC画图总结-DIB图形绘制
- linux重启用reboot后起不来_linux使用reboot重启后怎么办?
- 联通突然从4g变成3g了_老人机真不能用了?!联通逐渐关闭2G、3G信号服务
- linux 文件乱码_RedHatlinux系统虚机启动后/etc/fstab中的文件系统未能挂载上
- 转 常用C#正则表达式收集。
- 【ML小结7】贝叶斯分类器:朴素、半朴素贝叶斯
- 苹果手机android解锁,解锁教程:Android和iOS手机锁屏密码忘了怎么办?
- java关于时间的换算
- 怎么把excel转换成word
- 万物智联下,腾讯云IoT的差异化发展之路“新”在何处?
- 福利来了,python 淘宝旗舰店、天猫、抢茅台,干死黄牛党《本人亲测有效》