Soundpool主要用于游戏中播放无延时的短促音效,在开发中应该将长度小于7s的声音资源放进SoundPool中,否则可能加载失败或内存占用过大。
SoundPool构造器及常用方法如下:
构造器或方法名 | 方法含义 | 各个参数含义 |
SoundPool(int maxStreams, int streamType, int srcQuality) | 构造函数 | maxStreams:用于设置同时能够播放多少个音效 streamType:音频类型,在游戏中通常设置为AudioManager.STREAM_MUSIC srcQuality:音频质量,目前没有作用,默认值0 |
加载音频文件 | Context:上下文 resId:音效资源ID Priority:优先级,目前没有作用,默认值1 | |
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) | 播放音效 | soundID:要播放的音效id leftVolume:左声道音量range = 0.0 to 1.0 rightVolume:右声道音量range = 0.0 to 1.0 Priority:优先度,0为最低 Loop:音效播放的循环次数,0为不循环,-1为永远循环 Rate:音效的回放速度,该值在.0.5-->2.0f,1.0为正常速度 |
pause(int streamID) | 暂停音效 | streamID:音效ID |
stop(int streamID) | 停止音效 | streamID:音效ID |