Soundpool of Android Buffet
- Soundpool of Android Buffet
- Soundpool Introduction
- Main methods
- How to use
- Using the example
SoundPool介绍
- Soundpool plays a short but high response speed and plays multiple sounds at the same time as opposed to MediaPlayer.
- The soundpool uses a separate thread to load the sound, and the Soundpool.onloadcompletelistener callback loads the completed method.
Main methods
- Loads the sound. The return value is Soundid.
Invalid priority parameter, 1
if the file is larger than 1M, only the first 1M is loaded
int load(AssetFileDescriptor afd, int priority) //通过一个AssetFileDescriptor对象
int load(Context context, int resId, int priority) //通过一个资源ID
int load(String path, int priority) //通过指定的路径加载
int load(FileDescriptor fd, long offset, long length, int priority) //通过FileDescriptor加载
- Playback controls.
final int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) //播放指定音频的音效,并返回一个streamID 。
//priority —— 流的优先级,值越大优先级高,影响当同时播放数量超出了最大支持数时SoundPool对该流的处理;
//loop —— 循环播放的次数,0为值播放一次,-1为无限循环,其他值为播放loop+1次(例如,3为一共播放4次).
//rate —— 播放的速率,范围0.5-2.0(0.5为一半速率,1.0为正常速率,2.0为两倍速率)
final void pause(int streamID) //暂停指定播放流的音效(streamID 应通过play()返回)。
final void resume(int streamID) //继续播放指定播放流的音效(streamID 应通过play()返回)。
final void stop(int streamID) //终止指定播放流的音效(streamID 应通过play()返回)。
- Frees resources.
final boolean unload(int soundID) //卸载一个指定的音频资源.
final void release() //释放SoundPool中的所有音频资源.
How to use
- Constructing a sound pool
SoundPool soundPool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
- Prepare the sound set
Map<Integer, Integer> soundMap=new HashMap<Integer, Integer>();
soundMap.put(1, soundPool.load(MainActivity.this, R.raw.ir_begin, 1));
soundMap.put(2, soundPool.load(MainActivity.this, R.raw.ir_end, 1));
soundMap.put(3, soundPool.load(MainActivity.this, R.raw.ir_inter, 1));
soundMap.put(4, soundPool.load(MainActivity.this, R.raw.tada, 1));
soundMap.put(5, soundPool.load(MainActivity.this, R.raw.zhong, 1));
- Play an audio
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
Using the example
Public class mainactivity extends Activity { //1. Prepare the resource file. Create a new raw subdirectory in the RES resource directory to put the audio files that need to be loaded, such as loading the Shiqishidaibeijingyinyue.mp3 PrivateSoundpool Soundpool;Private intSoundid;Private BooleanFlag =false;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//2. Implementing Soundpool ObjectsSoundpool =NewSoundpool (5, Audiomanager.stream_music,0);number of sound pools, audiomanager.stream_music and 0, respectively //3. Use Soundpool to load the sound, the operation bit asynchronous operation, if the resource is large, it takes some timeSoundid = Soundpool.load ( This, R.raw.shiqishidaibeijingyinyue,1);//4. Setting the load for the sound pool to complete the listener eventSoundpool.setonloadcompletelistener (NewSoundpool.onloadcompletelistener () {@Override Public void Onloadcomplete(Soundpool Soundpool,intSampleID,intStatus) {flag =true;//indicates loading complete} }); }//5. Playback Actions Public void Click(View view) {if(flag) {//Play the files in the sound pool, you can specify the playback volume, the rate of priority sound playbackSoundpool.play (Soundid,1.0F0.5F1,0,1.0f); }Else{Toast.maketext ( This,"Loading, please later",1). Show (); } }//6. After use is complete, the resource should be freed @Override protected void OnDestroy() {soundpool.release (); Soundpool =NULL;Super. OnDestroy (); } }
Soundpool of Android Buffet