Previously learned MediaPlayer used to play mobile music, but in the phone a lot of tone is not used MediaPlayer to play such as SMS ringtone, notification ringtone, Android using Soundpool to play small audio files;
Learn the use of Soundpool together below
"1" Set a button to play the start of the first interface
"2" Get Soundpool
Look at the API and learn that there are two ways to get soundpool depending on the version
Used before version 21
Soundpool pool =new soundpool (int maxstreams, int streamtype, int srcquality)
After version 21: Use Builder to generate
"3" calls the Load method to load the audio
"4" Call the Play method
Public final int Play (int soundid, float leftvolume, float rightvolume, int. priority, Int. loop, float rate)
Parameter introduction
Parameters
Soundid a soundid returned by the load () function
Leftvolume left volume value (range = 0.0 to 1.0)
Rightvolume Right Volume value (range = 0.0 to 1.0)
Priority Stream Precedence (0 = lowest priorities)
Loop loop Mode (0 = no loop, 1 = Loop forever) Cycle or not
Rate Playback Rates (1.0 = Normal playback, range 0.5 to 2.0) play back the speed
Precautions
If you don't have a sound, it's because it takes time to load the audio, don't put load and play together
Do not hibernate the thread, lest it hinder the main threads
Here is the source code
Public class mainactivity extends Activity { PrivateButton Mbutton;PrivateSoundpool pool =NULL;Private intId@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mbutton = (Button) Findviewbyid (R.id.button_sound);//Must be on the outside because it takes time to load the audio or there will be no soundId= Initsound (); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {PlaySound (); } }); }Private void PlaySound() {//public final int play (int soundid, float leftvolume, float rightvolume, int priority, Int. loop, float rate)//Parameters//Soundid a soundid returned by the load () function//Leftvolume Left volume value (range = 0.0 to 1.0)//Rightvolume Right volume value (range = 0.0 to 1.0)//Priority stream Precedence (0 = lowest priorities)//Loop loop mode (0 = no loop,-1 = Loop forever) loop or not//Rate Playback rates (1.0 = Normal playback, range 0.5 to 2.0) play back SpeedPool.play (ID,1,1,0, -1,1); } Public int Initsound() {if(Build.VERSION.SDK_INT > +) {Soundpool.builder Builder =NewSoundpool.builder (); Builder.setmaxstreams (2); Audioattributes.builder Builder1 =NewAudioattributes.builder (); Builder1.setlegacystreamtype (Audiomanager.stream_music); Builder.setaudioattributes (Builder1.build ()); Pool = Builder.build (); }Else{//21 version previously used soundpool (int maxstreams, int streamtype, int srcquality)Pool =NewSoundpool (2, Audiomanager.stream_music,0); }returnPool.load (Getapplicationcontext (), r.raw.outgoing,1); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Multimedia--4 Step Learning Soundpool