Android audio play SoundPool usage and encapsulation, androidsoundpool
Generally, MediaPlayer is used to play audio. the creation and destruction of MediaPlayer consume a lot of resources, if we want to play short-lived and frequently-played audio, MediaPlayer is not suitable. Let's talk about playing short-lived audio in SoundPool:
The SoundPool structure is as follows:
Initialization
SoundPool
InitializationSoundPool
We directlynew SoundPool (int maxStreams, int streamType, int srcQuality)
You can.
Parameter description:
Parameters |
Explanation |
MaxStreams |
Maximum number of streams |
StreamType |
Stream type (SEE) SuggestionsAudioManager.STREAM_SYSTEM |
SrcQuality |
Frequency quality. The default value is 0, which is not affected currently. |
Important Methods
Load audio
Inload
In this method, we usually place the audio fileres
Ofraw
Folder, and then useload(Context context, int resId, int priority)
Method to load the audioSoundPool
Medium:
Parameter description:
Parameters |
Explanation |
Context |
Context |
ResId |
Audio file address: R. raw. deep |
Priority |
Priority: the priority is set to 1 if the audio is short. |
Play audio
It is relatively simple to play audio.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
Parameter description:
Parameters |
Explanation |
SoundID |
Sound id (that is, the sequence from load to SoundPool, starting from 1) |
LeftVolume \ rightVolume |
Volume Control for left/right audio channels, from 0.0 to 1.0 |
Priority |
Priority. 0 is the lowest priority. |
Loop |
Whether to play cyclically. 0 indicates no loop, and-1 indicates loop. |
Rate |
Playback ratio, from 0.5 to 2, generally 1, indicating normal playback |
Summary
The code used to play an audio video is as follows:
// Initialize SoundPoolprivate SoundPool soundPool = newSoundPool (10, AudioManager. STREAM_SYSTEM, 5); // load the deep audio file soundPool. load (this, R. raw. deep, 1); // play the deepsoundPool. play (1, 1, 0, 0, 1 );
Encapsulation
If we use audio files in many activities, for example, adding sound effects to all click operations, we need new for each Activity, and load the audio files in play, this is complicated and confusing, so we do the following sub-assembly:
/*** @ Author zsl * @ blog http://blog.csdn.net/yy1300326388 ***/public class SoundPlayUtils {// SoundPool object public static SoundPool mSoundPlayer = new SoundPool (10, AudioManager. STREAM_SYSTEM, 5); public static SoundPlayUtils soundPlayUtils; // Context static context mContext;/*** initialize ** @ param Context */public static SoundPlayUtils init (context Context) {if (soundPlayUtils = null) {soundPlayUtils = new SoundPlayUtils ();} // initialize the sound mContext = context; mSoundPlayer. load (mContext, R. raw. beng, 1); // 1 mSoundPlayer. load (mContext, R. raw. click, 1); // 2 mSoundPlayer. load (mContext, R. raw. diang, 1); // 3 mSoundPlayer. load (mContext, R. raw. ding, 1); // 4 mSoundPlayer. load (mContext, R. raw. gone, 1); // 5 mSoundPlayer. load (mContext, R. raw. popup, 1); // 6 mSoundPlayer. load (mContext, R. raw. water, 1); // 7 mSoundPlayer. load (mContext, R. raw. ying, 1); // 8 return soundPlayUtils;}/*** playback sound ** @ param soundID */public static void play (int soundID) {mSoundPlayer. play (soundID, 1, 1, 0, 0, 1 );}}
First, we load all the files. When using it, we can directly query and check whether the files are loaded. Then we can directly call the play method:
Use
Step 1: Add the following code to the onCreate method of the Activity at the program entry:
// Initialize the sound effect SoundPlayUtils. init (this );
Step 2: directly use the following code when playing anywhere:
// Play sound SoundPlayUtils. play (1 );
// Play sound SoundPlayUtils. play (7 );
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.