Android Play Sound

Source: Internet
Author: User

public class myactivity extends activity {      /**  Called when the activity is first created. */       SoundPool sp;                            //get a sound pool reference        HashMap<Integer,Integer> spMap;            //get a reference to a map       Button b1;                                //Sound Playback control button       button  b1Pause;                              //Voice Pause control button       Button b2;                                //Sound Playback control button        Button b2Pause;                              //Voice Pause Control button        @Override       public void  OnCreate (bundle savedinstancestate)  {           Super.oncreate (savedinstancestate);           setcontentview ( R.layout.main);           initsoundpool ();                         //Initializing the Sound pool            b1= (Button) Findviewbyid (r.id.button01);//Sound Playback control button instantiation           b2= (Button) Findviewbyid (R.ID.BUTTON02);// The Sound Playback control button instantiates the           b1pause= (button) Findviewbyid ( R.id.button1pause);           b2pause= (Button) findViewById ( R.id.button2pause);           b1.setonclicklistener (new  View.onclicklistener ()  {//button Register Click event Listener                 @Override                public void onclick (VIEW&NBSP;V)  {                 &nBsp; playsound (     //); play the first sound, cycle through                    toast.maketext (MyActivity.this,  " Play sound 1 ",  toast.length_short). Show ();           }});           b1pause.setonclicklistener (New View.OnClickListener ()  {               @Override                public void onclick ( VIEW&NBSP;V)  {                   sp.pause (Spmap.get (1));                   toast.maketext (myactivity.this,  "Pause sound 1",  Toast.LENGTH_ Short). Show ();           }});           b2.setonclicklistener (New view.onclicklistener ()  {                @Override                public void onclick (VIEW&NBSP;V)  {                   playsound (2,1);      //Play second sound, loop over                toast.maketext (myactivity.this,  "Play sound 2",  toast.length_short). Show ();           }});           B2pause.setonclicklistener (New view.onclicklistener ()  {               @Override              public void  onclick (VIEW&NBSP;V)  {                   sp.pause (Spmap.get (2));                   toast.maketext (myactivity.this,  "Pause sound 2",  toast.length_short). Show ();           });       }      public void initsoundpool () {             //Initializing the Sound pool            sp=new soundpool (                   5,               //maxstreams parameter, which sets how much sound can be played at the same time                 The    audiomanager.stream_music,  //streamtype parameter, which sets the audio type, is typically set to:stream_music  in the game                  0                //srcquality parameters, This parameter sets the quality of the audio file and currently has no effect, set to 0 as the default value.           );           spMap=new HashMap<Integer,Integer> ();           spmap.put (1, sp.load (this, r.raw.attack02, 1));//load the audio file and put it in the map!           spmap.put (2, sp.load (this,  r.raw.attack14, 1));      }      public  Void playsound (INT&NBsp;sound,int number) {    //plays the sound, the parameter sounds is the ID of the sound effect, and the parameter number is how many times the sound is played            audiomanager am= (Audiomanager) This.getSystemService (this. Audio_service);//Instantiate Audiomanager object           float  Audiomaxvolumn=am.getstreammaxvolume (Audiomanager.stream_music)   //returns the maximum volume value of the current AudioManager object            float audiocurrentvolumn=am.getstreamvolume ( AUDIOMANAGER.STREAM_MUSIC)//Returns the volume value of the current Audiomanager object            float volumnratio=audiocurrentvolumn/audiomaxvolumn;    //left and right channel volume;           sp.play (                   spmap.get (sound),                    //playing music id                   volumnRatio,                         //left channel volume                     volumnratio,                         //Right Channel volume                    1,                                    //priority, 0 for lowest                    number,                              //cycle times, 0 no cycle, 1 cycles forever                   1                                     //playback Speed &NBSP, the value is between 0.5-2.0 and 1 is normal speed           );       }  }


Android Play Sound

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.