Android authoritative Programming-notes (Chapter 19 using Soundpool to play audio)

Source: Internet
Author: User

For beatbox applications, you can use Soundpool, a specially customized utility.

Soundpool can load a batch of sound resources into memory and support simultaneous playback of multiple audio files. So, even if the user is excited, push the button to play all the audio, do not have to worry about damaging the application or consumption of the phone power.

1. Create Soundpool

 1  /*  2   * Lollipop introduces a new way to create Soundpool: Use Soundpool.builder. To be compatible with the lowest level of API 16, you can only choose to use the old construction method of soundpool (int int int).  3   * The first parameter specifies how many audio to play simultaneously, specifying 5. When playing 5 audio, Soundpool stops playing the original audio if you try to play a 6th.  4   * The second parameter determines the type of audio stream.  5   The last parameter specifies the sample rate conversion quality.  Not working, incoming 0;  6  */ 7  msoundpool = new  soundpool (max_sounds, AUDIOMANAGER.STREAM_ MUSIC, 0); 
8 //
New Soundpool.builder ()            . Setmaxstreams (Max_sounds)            . Setaudioattributes(new  Audioattributes.builder ().                    Setlegacystreamtype (audiomanager.stream_music). Build                    ())            . Build ();

2. Loading audio files

Compared to other audio playback methods, Soundpool also has a quick response advantage, when the command is first issued, it will start playing immediately, not a bit of procrastination. The price is that the audio must be preloaded before the money is played. Soundpool loaded audio files have their own integer type ID.

Add the Msoundid instance variable in the sound class and add the appropriate get method and set method to manage this write ID.

Then handle the audio load and add the following code to the Beatbox:

Private void loadthrows  ioexception{        = massets.openfd (Sound.getassetpath () );         // the load (assetfiledescriptor, int) method is called to load the file into the Soundpool        to be broadcast, // This method returns an int ID. This is the ID stored in the msoundid.         int soundid = msoundpool.load (afd,1);        Sound.setsoundid (Soundid);    }

Then in the Beatbox.loadsounds () method, call the load (sound) method to load all the audio files.

3. Play Audio

Add a Play (sound) method to the Beatbox.

1    PublicvoidPlay (sound) {2Integer Soundid =Sound.getsoundid ();3         //Check to make sure Soundid is not a null value, and a sound load failure causes Soundid to appear null. 4         if(Soundid = =NULL)5             return;6         /*7 * Call the Play method to play audio. 8 * These parameters are in order: Audio ID, left volume, right volume, priority (invalid), loop and playback rate. 9 * We need maximum volume and long-speed playback, so pass in 1.0, whether the loop parameter passed in 0 value, represents not loop. -1 represents the loop. Ten          */ OneMsoundpool.play (soundid,1.0f,1.0f,1,0,1.0f); A}

Finally, the button listener method is added to realize the Click button to play audio.

Android authoritative Programming-notes (Chapter 19 using Soundpool to play audio)

Related Article

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.