SoundPool
来源:互联网 发布:火车采集器软件 编辑:程序博客网 时间:2024/06/05 09:13
概述
播放一般较大内存的音乐,可以用MediaPlayer,但实际中,那些小型的音频或(如:提示音)或者对响应速度要求较高的音频就不适合用MediaPlayer,MediaPlayer的响应需要一定时间,而且需要添加线程。
SoundPool载入音乐文件使用了独立的线程,不会阻塞UI主线程的操作。但是这里如果音效文件过大没有载入完成,我们调用play方法时可能产生严重的后果,这里Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解媒体文件是否载入完成,我们重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可获得。 从上面的onLoadComplete方法可以看出该类有很多参数,比如类似id,是的SoundPool在load时可以处理多个媒体一次初始化并放入内存中,这里效率比MediaPlayer高了很多。 SoundPool类支持同时播放多个音效,这对于游戏来说是十分必要的,而MediaPlayer类是同步执行的只能一个文件一个文件的播放。SDK21版本后,SoundPool的创建发生了很大变化,所以在开发中需要进行版本控制。
使用方法
sdk21之前:
创建SoundPool:
SoundPool(int maxStream, int streamType, int srcQuality)
maxStream —— 同时播放的流的最大数量
streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)
srcQuality —— 采样率转化质量,当前无效果,使用0作为默认
加载音频:如果要加载多个音频,需要用到HashMap等,单个音频,直接传入load()方法即可
soundPool的加载 :
int load(Context context, int resId, int priority) //从APK资源载入
int load(FileDescriptor fd, long offset, long length, int priority) //从FileDescriptor对象载入
int load(AssetFileDescriptor afd, int priority) //从Asset对象载入 int load(String path, int priority) //从完整文件路径名载入播放音频:
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
sdk21之后:
麻烦很多,在代码中体现。
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<code
class
=
"language-java"
hljs=
""
>
public
class
MainActivity
extends
Activity {
private
Button mButtonSoundPool;
private
SoundPool mPool;
private
int
voiceID;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSoundPool = (Button) findViewById(R.id.button_sound_pool);
mButtonSoundPool.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
mPool.play(voiceID,
1
,
1
,
0
,-
1
,
1
);
}
});
voiceID = initSoundPool();
}
private
int
initSoundPool(){
/**
* 21版本后,SoundPool的创建发生很大改变
*/
//判断系统sdk版本,如果版本超过21,调用第一种
if
(Build.VERSION.SDK_INT>=
21
){
SoundPool.Builder builder =
new
SoundPool.Builder();
builder.setMaxStreams(
2
);
//传入音频数量
//AudioAttributes是一个封装音频各种属性的方法
AudioAttributes.Builder attrBuilder =
new
AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
//设置音频流的合适的属性
builder.setAudioAttributes(attrBuilder.build());
//加载一个AudioAttributes
mPool = builder.build();
}
else
{
mPool =
new
SoundPool(
2
,AudioManager.STREAM_MUSIC,
0
);
}
//load的返回值是一个int类的值:音频的id,在SoundPool的play()方法中加入这个id就能播放这个音频
return
mPool.load(getApplicationContext(),R.raw.outgoing,
1
);
}
}</code>
阅读全文
0 0
- soundpool
- SoundPool
- SoundPool
- soundpool
- soundpool
- SoundPool
- SoundPool
- SoundPool
- SoundPool
- SoundPool
- SoundPool
- SoundPool使用
- android - SoundPool
- MediaPlayer & SoundPool
- SoundPool使用
- Android SoundPool
- Android soundPool
- SoundPool小结
- Ubuntu16.04 安装流程 -4 编程-Git_PyCharm_TeX_VSCode(201709)
- Java I/O流操作
- 大数据之专业术语
- 线性筛
- LSTM中文评论情感分析(粗糙版)
- SoundPool
- my扩展欧几里得
- FileMaker Server 16 v16.0.1.184免费版下载附安装教程
- mysql优化(待补充版)
- 机器学习入门
- Tensorflow实例:mnist最佳实践
- 2.2数字音频压缩标准&2.3声卡与电声设备
- KMP算法(java实现)
- CodeForces #431 Div. 2 849C From Y to Y 贪心 技巧题