SoundPool usage for Android Development
Play Sound Effects using SoundPool
MediaPlayer is not suitable for applications that often play intensive, fast, and short-lived audio effects (such as game sound effects. MediaPlayer has the following Disadvantages:
1) The latency is long and the resource usage is high.
2) Multiple audios cannot be played simultaneously.
In addition to playing audio using MediaPlayer, Android also provides SoundPool for playing sound effects. SoundPool uses the sound effect pool to manage multiple short-term sound effects. For example, it can load 20 sound effects at the beginning, in the future, play the video by the sound ID in the program.
SoundPool is mainly used to play some shorter sound clips. Compared with MediaPlayer, SoundPool has the advantage of low CPU resource occupation and low response latency. In addition, SoundPool also allows you to set parameters such as sound quality, volume, and playback ratio.
SoundPool provides a constructor that specifies the total number of sounds (that is, the size of the pool) supported by the constructor and the quality of the sound. The constructor is as follows:
SoundPool (int maxStreams, int streamType, int srcQuality): the first parameter specifies the number of sounds supported. The second parameter specifies the sound type. The third parameter specifies the sound quality.
Once the SoundPool object is obtained, you can call multiple load methods of SoundPool to load the sound.
SoundPool provides the following four load methods:
Int load (Context context, int resld, int priority): loads sound from the resource corresponding to the resld.
Int load (FileDescriptor fd, long offset, long length, int priority): loads the sound of the start and length of the offset of the file corresponding to fd.
Int load (AssetFileDescriptor afd, int priority): loads sound from the file corresponding to afd.
Int load (String path, int priority): loads sound from the file corresponding to the path.
There is a priority parameter in the above four methods. This parameter does not work yet. Android recommends setting this parameter to 1 to maintain compatibility with the future.
After the above four methods load the sound, the ID of the sound will be returned, and then the program can play the specified sound through the sound ID.
The method provided by SoundPool for playing a specified sound:
Int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate): the first parameter of this method specifies which sound to play; leftVolume and rightVolume specify the volume on the left and right: priority specifies the playback sound priority. A greater value indicates a higher priority. loop indicates whether to loop. 0 indicates no loop, and-1 indicates a loop; rate specifies the playback rate. The value ranges from 0.5 to 2. 1 indicates the normal rate.
To better manage the 1D of each sound loaded by the SoundPool, the program generally uses a HashMap Object To manage the sound.
To sum up, follow these steps:
1) Call the SoundPool constructor to create the SoundPool object.
2) Call the load () method of the SoundPool object to load sound from a specified resource or file. It is best to use HashMap <Integer, Integer> to manage the loaded sound.
3) call the play method of SoundPool to play the sound.
The following program demonstrates how to use SoundPool to play sound effects.
The program code is as follows:
PublicClassSoundPoolDemoExtendsActivity { Button btn1, btn2, btn3; // Create a SoundPool object SoundPool soundPool; // Define a HashMap used to store the audio stream ID HashMap MusicId = NewHashMap (); @ Override ProtectedVoidOnCreate (Bundle savedInstanceState ){ Super. OnCreate (savedInstanceState ); SetContentView (R. layout.Mian); Btn1 = (Button) findViewById (R. id.Btn1); Btn2 = (Button) findViewById (R. id.Btn2); Btn3 = (Button) findViewById (R. id.Btn3); // Initialize soundPool and set to accommodate 12 audio streams. The audio stream quality is 5, SoundPool =NewSoundPool (12, 0, 5 ); // Load the specified audio stream using the load method and put the returned audio ID in musicId MusicId. put (1, soundPool. load (This, R. raw.Awooga, 1 )); MusicId. put (2, soundPool. load (This, R. raw.Evillaugh, 1 )); MusicId. put (3, soundPool. load (This, R. raw.Jackinthebox, 1 )); OnClickListener listener =NewOnClickListener (){ @ Override PublicVoidOnClick (View v ){ //TODOAuto-generated method stub Switch(V. getId ()){ CaseR. id.Btn1: // Play the specified audio stream SoundPool. play (musicId. get (1), 1, 1, 0, 0, 1 ); Break; CaseR. id.Btn2: SoundPool. play (musicId. get (2), 1, 1, 0, 0, 1 ); Break; CaseR. id.Btn3: SoundPool. play (musicId. get (3), 1, 1, 0, 0, 1 ); Break; Default: Break; } } }; Btn1.setOnClickListener (listener ); Btn2.setOnClickListener (listener ); Btn3.setOnClickListener (listener ); } } |
Program running:
Note the following when playing a sound using SoundPool:
Although SoundPool can load multiple sounds at a time, due to memory restrictions, it is recommended that you avoid using SoundPool to play songs or playing background music in games, only short and intensive sounds can be played using the SoundPool.
Although SoundPool is better than MediaPlayer, there is no absolute delay problem, especially in phones with poor performance, SoundPool latency problems will be more serious.