Android使用SoundPool播放短音效
作者:T9的第三个三角 时间:2022-03-02 12:33:00
前言
对于Android播放一些简短音效,例如提示音,或者铃声,相对于使用MediaPlayer,SoundPool可以节省更多资源,并且可以同时播放多个音效,而且可以针对不同音效设置不同播放品质
实现
SoundPool的具体作用,就不再阐述,直接贴代码
private SoundPool.Builder spBuilder;
private SoundPool soundPool;
private Integer[] fmSound = FmManager.getRawAudios();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (null == spBuilder) {
spBuilder = new SoundPool.Builder();
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
spBuilder.setAudioAttributes(builder.build());
spBuilder.setMaxStreams(10);
}
if (null == soundPool) {
soundPool = spBuilder.build();
}
} else {
if (null == soundPool) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 10); //最多播放10个音效,格式为Steam_music,音质为10
}
}
soundPool.setOnLoadCompleteListener(this);
if (null == fmArray) {
fmArray = new SparseIntArray();
}
if (null == streamArray) {
streamArray = new SparseIntArray();
}
for (int i = 0; i < fmSound.length; i++) {
fmArray.put(i + 1, soundPool.load(this, fmSound[i], 1)); //将需要播放的资源添加到SoundPool中,并保存返回的StreamID,通过StreamID可以停止某个音效
}
private void playFmByPosition(int resultId) {
if (null == soundPool || resultId < 0 || fmArray == null || fmArray.size() < 0 || streamArray == null)
return;
LogUtils.e(resultId + "------------" + fmArray.size());
if (resultId < fmArray.size()) {
if (!FmPlaying.isPlay(resultId)) {
int fmPlayId = soundPool.play(fmArray.get(resultId + 1), 1, 1, 0, -1, 1);
streamArray.put(resultId, fmPlayId);
FmPlaying.setPlay(resultId, true);
} else {
soundPool.stop(streamArray.get(resultId));
streamArray.removeAt(resultId);
FmPlaying.setPlay(resultId, false);
}
}
}
static class FmPlaying {
private static SparseBooleanArray playArray = new SparseBooleanArray();
public static boolean isPlay(int position) {
return playArray.get(position, false);
}
public static void setPlay(int position, boolean play) {
playArray.put(position, play);
}
}
来源:https://blog.csdn.net/dazhaoDai/article/details/73650589
标签:Android,SoundPool,短音效
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android重力传感器实现滚动的弹球
2023-05-04 05:49:28
![](https://img.aspxhome.com/file/2023/7/102597_0s.png)
C#版Tesseract库的使用技巧
2023-01-24 20:07:53
![](https://img.aspxhome.com/file/2023/0/108730_0s.png)
基于Android中的 AutoCompleteTextView实现自动填充
2023-04-02 07:11:36
java结合email实现自动推送功能
2023-07-09 00:16:43
mybatis输出SQL格式化方式
2021-06-18 18:19:45
springboot整合vue实现上传下载文件
2023-11-14 07:10:37
![](https://img.aspxhome.com/file/2023/4/94374_0s.jpg)
SpringBoot Java后端实现okhttp3超时设置的方法实例
2022-11-06 04:56:03
C#预处理器指令的用法实例分析
2023-03-09 16:21:07
Java8 Comparator: 列表排序的深入讲解
2022-12-11 08:30:01
java导出Excel通用方法的实例详解
2022-06-25 14:28:25
C#实现获取机器码的示例详解
2021-11-13 06:57:19
![](https://img.aspxhome.com/file/2023/6/80096_0s.png)
Java 反射类型Type的用法说明
2023-01-23 04:15:44
java必学必会之线程(2)
2023-11-09 10:22:35
![](https://img.aspxhome.com/file/2023/2/58742_0s.png)
java组件commons-fileupload实现文件上传、下载、在线打开
2022-02-24 22:16:59
![](https://img.aspxhome.com/file/2023/9/97789_0s.gif)
C#中的引用类型以及特殊引用类型详解
2023-06-18 01:43:46
![](https://img.aspxhome.com/file/2023/3/100163_0s.png)
如何优雅的处理Spring Boot异常信息详解
2023-11-29 09:50:02
![](https://img.aspxhome.com/file/2023/6/60536_0s.jpg)
Android实现拍照及图片裁剪(6.0以上权限处理及7.0以上文件管理)
2022-05-19 09:18:14
springcloud之自定义简易消费服务组件
2022-01-29 00:18:24
![](https://img.aspxhome.com/file/2023/8/61878_0s.png)
static关键字有何魔法?竟让Spring Boot搞出那么多静态内部类(推荐)
2022-07-03 20:26:29
![](https://img.aspxhome.com/file/2023/9/70269_0s.png)
Spring boot jpa 删除数据和事务管理的问题实例详解
2022-07-02 12:11:43