SoundPool 音频播放 详解 示例
官方文档
API 介绍
- SoundPool(int maxStreams, int streamType, int srcQuality) This constructor was deprecated in API level 21. use SoundPool.Builder instead to create and configure a SoundPool instance
- maxStream:同时播放的流的最大数量
- streamType:流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)
- srcQuality:采样率转化质量,当前无效果,使用0作为默认值
- static void deprecateStreamTypeForPlayback(int streamType, String className, String opName)
- Use to generate warning or exception in legacy code paths that allowed passing stream types to qualify audio playback. 用于在旧代码路径中生成警告或异常,允许传递流类型限定音频播放。
- int soundID load(Context context, int resId, int priority) Load the sound from the specified APK resource.
- Note that the extension is dropped. For example, if you want to load a sound from the raw resource file "explosion.mp3", you would specify "R.raw.explosion" as the resource ID. Note that this means you cannot have both an "explosion.wav" and an "explosion.mp3" in the res/raw directory. 请注意,扩展名被删除。 例如,如果要从raw资源文件“explosion.mp3”加载声音,则应该指定“R.raw.explosion”作为资源ID。 请注意,这意味着您在res / raw目录中不能同时拥有“explosion.wav”和“explosion.mp3”文件。
- int priority: the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
- Returns: a sound ID. This value can be used to play or unload the sound.
- int soundID load(String path, int priority) Load the sound from the specified path.
- int soundID load(AssetFileDescriptor afd, int priority) Load the sound from an asset file descriptor.
- int soundID load(FileDescriptor fd, long offset, long length, int priority) Load the sound from a FileDescriptor.
- This version is useful if you store multiple sounds in a single binary. The offset specifies the offset from the start of the file and the length specifies the length of the sound within the file. 如果您将多个声音存储在单个二进制文件中,则此版本很有用。 offset指定从文件开头的偏移量,length指定文件中声音的长度。
- final int streamID play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) Play a sound from a sound ID.
- final void pause(int streamID) Pause a playback stream. 暂停播放
- Pause the stream specified by the streamID. This is the value returned by the play() function. If the stream is playing, it will be paused. If the stream is not playing (e.g. is stopped or was previously paused), calling this function will have no effect. 暂停streamID指定的流。 这是play()函数返回的值。 如果流正在播放,它将被暂停。 如果流未播放(例如,停止或先前已暂停),则调用此功能将不起作用。
- final void resume(int streamID) Resume a playback stream. 继续播放
- Resume the stream specified by the streamID. This is the value returned by the play() function. If the stream is paused, this will resume playback. If the stream was not previously paused, calling this function will have no effect.
- final void stop(int streamID) Stop a playback stream. 终止播放
- Stop the stream specified by the streamID. This is the value returned by the play() function. If the stream is playing, it will be stopped. It also releases any native resources associated with this stream. If the stream is not playing, it will have no effect.
- final void setLoop(int streamID, int loop) Set loop mode. 设置指定播放流的循环次数
- A loop value of -1 means loop forever, a value of 0 means don't loop, other values indicate the number of repeats, e.g. a value of 1 plays the audio twice. If the stream does not exist, it will have no effect.
- final void setPriority(int streamID, int priority) Change stream priority. 设置指定播放流的优先级
- Change the priority of the stream specified by the streamID. This is the value returned by the play() function. Affects the order in which streams are re-used to play new sounds. If the stream does not exist, it will have no effect.
- final void setRate(int streamID, float rate) Change playback rate. 设置指定播放流的播放速率
- The playback rate allows the application to vary the playback rate (pitch) of the sound. A value of 1.0 means playback at the original frequency. A value of 2.0 means playback twice as fast, and a value of 0.5 means playback at half speed. If the stream does not exist, it will have no effect.
- final void setVolume(int streamID, float leftVolume, float rightVolume) Set stream volume. 设置指定播放流的音量大小
- Sets the volume on the stream specified by the streamID. This is the value returned by the play() function. The value must be in the range of 0.0 to 1.0. If the stream does not exist, it will have no effect.
- final void autoPause() Pause all active streams.
- Pause all streams that are currently playing. This function iterates through all the active streams and pauses any that are playing. It also sets a flag so that any streams that are playing can be resumed by calling autoResume(). 暂停正在播放的所有流。 此函数遍历所有活动流并暂停播放任何正在播放的流。 它还设置一个标志,以便可以通过调用autoResume()来恢复正在播放的任何流。
- final void autoResume() Resume all previously active streams.
- Automatically resumes all streams that were paused in previous calls to autoPause().
- final boolean unload(int soundID) Unload a sound from a sound ID. 卸载一个指定的音频资源
- Unloads the sound specified by the soundID. This is the value returned by the load() function. Returns true if the sound is successfully unloaded, false if the sound was already unloaded.
- final void release() Release the SoundPool resources. 释放所有资源
- Release all memory and native resources used by the SoundPool object. The SoundPool can no longer be used and the reference should be set to null.
- void setOnLoadCompleteListener(SoundPool.OnLoadCompleteListener listener) Sets the callback hook for the OnLoadCompleteListener.
- void onLoadComplete(SoundPool soundPool, int sampleId, int status) Called when a sound has completed loading.
- SoundPool.Builder() Constructs a new Builder with the defaults format values.
- If not provided, the maximum number of streams is 1 , and the audio attributes have a usage value of USAGE_MEDIA (see setAudioAttributes(AudioAttributes) to change them).
- SoundPool.Builder setAudioAttributes(AudioAttributes attributes) Sets the AudioAttributes.
- Sets the AudioAttributes. For examples, game applications will use attributes built with usage information set to USAGE_GAME.
- AudioAttributes:A class to encapsulate a collection of attributes describing information about an audio stream. 封装描述有关音频流的信息的属性集合的类。
- SoundPool.Builder setMaxStreams(int maxStreams) Sets the maximum of number of simultaneous streams that can be played simultaneously.
- SoundPool build()
加载与播放方法
- 通过一个资源ID,int load(Context context, int resId, int priority)
- 通过指定的路径,int load(String path, int priority)
- 通过一个AssetFileDescriptor对象,int load(AssetFileDescriptor afd, int priority)
- 通过FileDescriptor,int load(FileDescriptor fd, long offset, long length, int priority)
final int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
- soundID int: a soundID returned by the load() function
- leftVolume float: left volume value (range = 0.0 to 1.0) 左声道的音量
- rightVolume float: right volume value (range = 0.0 to 1.0)
- priority int: stream priority (0 = lowest priority) 流的优先级
- loop int: loop mode (0 = no loop, -1 = loop forever)
- rate float: playback rate (1.0 = normal playback, range 0.5 to 2.0) 播放的速率
- int non-zero streamID if successful, zero if failed
个人对SoundPool的认识与总结
- 1) 延时时间较长,且资源占用率高。
- 2) 不支持多个音频同时播放。
- SoundPool最大只能申请1M的内存空间,这就意味着我们只能用一些很短的声音片段,而不是用它来播放歌曲或者做游戏背景音乐。
- SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用,因为有些时候它们可能会使你的程序莫名其妙的终止。有些朋友反映它们不会立即中止播放声音,而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。
- 流的加载过程是一个将音频解压为原始16位PCM数据的过程,由一个后台线程来进行异步处理,所以初始化后不能立即播放,需要等待一点时间。
- SoundPool的效率在这些播放类中算是很好的了,但也不是绝对不存在延迟问题,尤其在那些性能不太好的手机中,SoundPool的延迟问题可能会很严重。
- 1、一个SoundPool可以管理多个音频资源,通过load()函数,成功则返回非0的soundID
- 2、一个SoundPool可以同时播放多个音频资源,通过play()函数,成功则返回非0的streamID
- 3、pause()、resume()和stop()等操作是针对streamID(播放流)的
- 4、当设置为无限循环时,需要手动调用stop()来终止播放
- 5、播放流的优先级(play()中的priority参数),只在同时播放数超过设定的最大数时起作用
- 6、程序中不用考虑播放流的生命周期,无效的soundID/streamID不会导致程序错误
演示示例
public class SoundPoolActivity extends ListActivity {
private SoundPool soundPool;
private List<Integer> soundIdList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = {"0、raw:s10_bgm.ogg",
"1、raw:cf_bgm1.mp3",
"2、SD卡:s10_bgm.ogg",
"3、URL:s10_bgm.ogg,网络URL,不能播放",
"4、AssetFileDescriptor:assets目录",
"5、AssetFileDescriptor:raw目录",
"6、FileDescriptor:不能播放",
"7、FileDescriptor:不能播放",};
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(Arrays.asList(array))));
soundPool = new SoundPool.Builder()
.setMaxStreams(5)
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build())
.build();
soundPool.setOnLoadCompleteListener((soundPool1, sampleId, status) -> {
Log.i("bqt", "【onLoadComplete】" + sampleId + " " + status);//status : 0 = success
});
Context context = this.getApplicationContext();//不管使用哪个Context,退出当前Activity后都会且会在延迟几秒钟后停止播放
for (int i = 0; i < 8; i++) {
soundIdList.add(-1);
}
//通过一个资源ID:Context context, int resId, int priority
soundIdList.set(0, soundPool.load(context, R.raw.s1_ding, 0));
soundIdList.set(1, soundPool.load(context, R.raw.cf_bgm1, 0));
//通过指定的路径:String path, int priority
soundIdList.set(2, soundPool.load(Environment.getExternalStorageDirectory() + File.separator + "s10_bgm.ogg", 0));
soundIdList.set(3, soundPool.load("http://www.baiqiantao.xyz/s10_bgm.ogg", 0));//网络路径,不能播放
//通过FileDescriptor:FileDescriptor fd, long offset, long length, int priority
try {
AssetFileDescriptor afd = getAssets().openFd("s10_bgm.ogg");
Log.i("bqt", "【afd】" + afd.getStartOffset() + "," + afd.getLength() + "," + afd.getDeclaredLength());//43868,42987,42987
soundIdList.set(4, soundPool.load(afd, 0));
AssetFileDescriptor afd2 = getResources().openRawResourceFd(R.raw.s1_ding);
Log.i("bqt", "【afd2】" + afd2.getStartOffset() + "," + afd2.getLength() + "," + afd2.getDeclaredLength());//4300928,60647,60647
soundIdList.set(5, soundPool.load(afd2, 0));
} catch (IOException e) {
e.printStackTrace();
}
//通过FileDescriptor:FileDescriptor fd, long offset, long length, int priority
try {
FileDescriptor fd = getAssets().openFd("s1_fire.ogg").getFileDescriptor();
Log.i("bqt", "【是否有效 1】" + fd.valid());//true
soundIdList.set(6, soundPool.load(fd, 0, 1, 0));//Unable to load sample 【onLoadComplete】5 -2147483648
} catch (IOException e) {
e.printStackTrace();
}
FileDescriptor fd2 = getResources().openRawResourceFd(R.raw.s1_fire).getFileDescriptor();
Log.i("bqt", "【是否有效 2】" + fd2.valid());//true
soundIdList.set(7, soundPool.load(fd2, 0, 1, 0));//Unable to load sample 【onLoadComplete】5 -2147483648
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int soundID = soundIdList.get(position);
Toast.makeText(this, "" + soundID, Toast.LENGTH_SHORT).show();
soundPool.play(soundID, 1, 1, 1, 0, 1);
}
}
SoundPool 音频播放 详解 示例相关推荐
- 微信小程序html5音频,微信小程序 audio音频播放详解及实例
loop:是否循环播放 id:标注唯一组件以this.audioCtx = wx.createAudioContext('myAudio')获取控制组件的对象. bindplay:播放时触发该事件 b ...
- python爬取app中的音频_Python爬取喜马拉雅音频数据详解
码农公社 210.net.cn 210是何含义?10月24日是程序员节,1024 =210.210既 210 之意. Python爬取喜马拉雅音频数据详解 一.项目目标 爬取喜马拉雅音频数据 受害 ...
- ffmpeg从某站提取视频、音频、详解
ffmpeg从某站提取视频.音频.详解 事件背景 准备链接 第一步安装下载 ffmpeg是开源软件,安装网址http://ffmpeg.org/download.html#build-windows ...
- AAC音频格式详解和实战解析
AAC音频格式详解和实战解析 一.基本概念 AAC:即MPEG-2 Advanced Audio Coding,分为流格式和文件格式.文件格式主要用于文件存储和文件播放,流格式主要用于流媒体在线播放. ...
- python爬取喜马拉雅_Python爬虫实战案例之爬取喜马拉雅音频数据详解
这篇文章我们来讲一下在网站建设中,Python爬虫实战案例之爬取喜马拉雅音频数据详解.本文对大家进行网站开发设计工作或者学习都有一定帮助,下面让我们进入正文. 前言 喜马拉雅是专业的音频分享平台,汇集 ...
- 小猫爪:嵌入式小知识10-I2S,TDM,PCM等音频格式详解
小猫爪:嵌入式小知识10-I2S,TDM,PCM等音频格式详解 1 前言 2 I2S 3 Codec模式(左/右对齐) 3.1 左对齐(MSB对齐) 3.2 右对齐(LSB对齐) 4 DSP模式 5 ...
- 【机器学习】【隐马尔可夫模型-3】后向算法:算法详解+示例讲解+Python实现
0.前排提示 csdn有些数学公式编辑不出来,所以本博用容易书写的表达式来表示专业数学公式,如: (1) 在本博客中用α<T>(i)来表示 (2)在本博客中用[i=1, N]∑来表示 注 ...
- Android通过OpenSL ES播放音频套路详解
我的视频课程(基础):<(NDK)FFmpeg打造Android万能音频播放器> 我的视频课程(进阶):<(NDK)FFmpeg打造Android视频播放器> 我的视频课程(编 ...
- Android:SharedPreferences详解+示例
为什么80%的码农都做不了架构师?>>> 一.简介 SharedPreferences是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储 ...
最新文章
- 传统编译器与神经网络编译器
- 深入理解ROS技术 【4】ROS下的模块详解(181-232)
- mysql用户管理+pymysql模块
- docker4dotnet #1 – 前世今生 amp; 世界你好
- 如何有效创建工作分解结构?
- gensim实现TF-IDF和LDA模型、sklearn实现聚类
- html编辑器菜鸟工具,富文本编辑器TinyMCE菜鸟使用教程
- 怎么在html中加入特效汉字,如何在小视频上加文字,文字动画特效,朋友圈小视频加文字...
- kali linux wine乱码,Kali安装Wine
- 国内公有云对比(1.5)- 功能篇之青云
- 沟通的艺术与处世智慧 ——戴尔卡耐基(笔记)
- CSDN竞赛6期题解
- 关于U盘烧录iso问题
- IDEA中如何设置滚轮缩放字体大小
- Win11打字不显示选字框怎么办?Win11打字不显示选字框的解决方法
- iperf3.1源码解析笔记(1)-网络压力测试工具iperf的使用
- Android快速实现手机号码识别OCR
- 【论文笔记】VOLO: Vision Outlooker for Visual Recognition
- WPF Excel导入01
- darknet训练时计算mAP和测试计算mAP