Android Multimedia--4 Step Learning Soundpool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.