When developing android software, you often need to play multimedia audio files. This type of operation is usually performed using the MediaPlayer class. However, the MediaPlayer class consumes a large amount of resources, which can reduce the performance of games and other applications. In Android, The SoundPool class is used to play the audio. The SoundPool class consumes less CPU resources and responds quickly.
SoundPool uses MediaPlaybackService to decode the audio stream into a 16-bit PCM mono-channel or stereo stream. This helps reduce the application latency caused by decoding.
Common SoundPool methods include:
Load () // load the audio file
Pause () // pause
Play () // play
Resume () // restore
SetLoop () // set the loop mode
SetOnLoadCompleteListener () // sets the listener
SetVolume () // sets the volume
SetRate () // sets the playback rate
Compared with other audio playback categories, SoundPool allows you to set the quality, volume, and playback rate during audio playback. In addition, it can manage multiple audio streams, each of which has its own independent ID. The management of a single audio stream is carried out by its ID. SoundPool is applicable to scenarios such as sound effects in applications (key tone prompts, messages, etc.) and intensive and transient sounds in the game (such as simultaneous explosion of multiple ships ).
The SoundPool class is used as follows:
Code 10-5 general process of playing audio in SoundPool
Int srcQuality = 100;
Int waitMsec = 1000;
Float leftVolume = SILENT;
Float rightVolume = volume;
Int priority = 1;
Int loop = 0;
Float rate = 1f;
SoundPool mSoundPool = new SoundPool (SOUNDPOOL_STREAMS, AudioManager. STREAM_MUSIC, srcQuality );
Int sampleId1 = mSoundPool. load (mContext, SOUND_A, PRIORITY );
Int streamID = mSoundPool. play (sampleID, leftVolume, rightVolume, priority, loop, rate );
......
Below is an example provided by a website: SoundPool integration in Activity
Import android. media. AudioManager;
Import android. media. SoundPool;
Public class android123 extends Activity {
Private SoundPool snd;
Private int hitOkSfx;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Snd = new SoundPool (10, AudioManager. STREAM_SYSTEM, 5 );
HitOkSfx = snd. load (context, R. raw. OK, 0 );
Button btn = (Button) findViewById (R. id. btn );
Btn. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
Snd. play (hitOkSfx, 1, 1, 0, 0, 1 );
}
);
}
}
SoundPool uses the setLoop (int streamID, int loop) method to set the playback mode. When the loop value is "-1", the playback mode is the loop mode; when the loop value is "0", the playback mode is single. When the loop value is a positive integer, the loop value indicates the number of playbacks.
SoundPool uses the setRate (int streamID, float rate) method to set the playback rate, which ranges from 0.5 ~ 2.0. When the rate is 2.0, it means that the current playback speed is twice the original rate and is in fast forward mode.
SoundPool uses setVolume (int streamID, float leftVolume, float rightVolume) to set the volume range of the audio stream.
In addition, through the play () method, SoundPool can set the playback mode, playback rate, and volume range at one time.
Note that SoundPool currently has some limitations, such as a maximum of 1 MB of memory space. SoundPool also provides the pause () and stop () methods, however, these methods currently have some bugs. We recommend that you do not use them easily. SoundPool still has some efficiency problems.