SoundPool 一般用来播放密集,急促而又短暂的音效。
SoundPool 对象可以看作是一个可以从 APK 中导入资源 或者从文件系统中载入文件的样本集合。
它利用 MediaPlayer 服务为音频解码为一个原始16位 PCM流。这个特性使得应用程序可以进行流压缩,而无须忍受在播放音频时解压所带来的 CPU 负载和延时。SoundPool 使用音效池的概念来管理多个播放流,如果超过流的最大数目, SoundPool 会基于优先级自动停止先前播放的流,另外,SoundPool 还支持自行设置声音的品质、 音量、 播放比率等参数。
在我们车机能想到的使用场景就是播放beep音和警告音等用户提示音。
常用方法:
方法 | 描述 |
---|---|
SoundPool(int maxStreams, int streamType, int srcQuality) |
构造函数 |
int load(Context context, int resId, int priority) int load(String path, int priority) int load(FileDescriptor fd, long offset, long length, int priority) int load(AssetFileDescriptor afd, int priority) |
加载声音资源,返回一个声音的ID,后面我们可以通过这个ID来播放指定的声音 |
int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) |
播放 |
void setVolume(int streamID, float volume) |
设置音量 |
void setPriority(int streamID, int priority) |
设置优先级 |
void pause(int streamID) |
暂停 |
void resume(int streamID) |
重播 |
void stop(int streamID) |
停止 |
void release() |
释放资源 |
参数介绍:
maxStreams 同时播放流的最大数量,当播放的流的数目大于此值,则会选择性停止优先级较低的流
streamType 流类型,比如 STREAM_MUSIC
srcQuality 采样率转换器质量,目前没有什么作用,默认填充0
context:上下文
resId:资源id
priority:没什么用的一个参数,建议设置为1,保持和未来的兼容性
path:文件路径
FileDescriptor:貌似是流吧,这个我也不知道
AssetFileDescriptor:从asset目录读取某个资源文件,用法: AssetFileDescriptor descriptor = - assetManager.openFd(“biaobiao.mp3”);
soundID:Load()返回的声音ID号
leftVolume:左声道音量设置