Play sound effects with Soundpool

Source: Internet
Author: User

Dense, short sounds are often required for applications because MediaPlayer has the following drawbacks:

1. Resource consumption is high, the delay time is longer.

2. Multiple sound effects are not supported at the same time.

Soundpool uses the concept of a sound pool to manage multiple short sounds, such as the ability to start with 20 sound effects, and then play them in the program by the ID of the sound.

Soundpool is mainly used to play a few short sound clips, compared to MediaPlayer, the advantage of Soundpool is that the CPU resource consumption is low and the reflection delay is small. In addition, Soundpool also supports setting the sound quality, volume, playback ratio and other parameters.

Soundpool provides a constructor that can specify how many sounds it supports (that is, the size of the pool), the quality of the sound, and so on.

Soundpool (int maxstreams, int streamtype, int srcquality): The first parameter specifies how many sounds are supported, the second parameter specifies the type of sound, and the third parameter specifies the sound quality.

Once you have the Soundpool object, you can then call the multiple overloaded load methods of Soundpool to load the sound:

int load (context context, int resid, int priority): Loads the sound from the resource corresponding to the RESID.

int load (filedescriptor fd, long offset, long length, int priority): Loads the offset of the file corresponding to FD and the length of the sound.

int load (assetfiledescriptor afd, int priority): Loads the sound from the file corresponding to the AfD.

int load (String path, int priority): Loads the sound from the file corresponding to path.

The above 4 methods have a priority parameter, which currently does not have any effect, Android recommends that the parameter is set to 1, maintain and future compatibility.

After the above 4 methods load the sound, the ID of the sound is returned, and later the program can play the specified sound through the ID of the sound, and Soundpool provides a way to play the specified sound as follows:

int play (int soundid, float leftvolume, float rightvolume, it priority, Int. Loop, Fl1oat rate): One parameter of the method specifies which sound is played; Leftvolume, Rightvolume specifies the volume of the left and right, priority specifies the precedence of the play sound, the higher the value, the greater the priority, the loop specifies whether to loop, 0 is not circular, 1 is the loop, and the rate specifies the ratio of playback, The values can be from 0.5 to 2,1 for normal ratios.

To better manage the ID of each sound loaded by Soundpool, the program typically uses a Hashmap<integer, integer> object to manage the sound.

To sum up, the steps to play the sound using Soundpool are as follows:

1. Call the Soundpool constructor to create the Soundpool object.

2. Call the Load () method of the Soundpool object to load the sound from the specified resource, file, preferably using the Hashmap<integer,integer> to manage the loaded sound.

3. Call the Soundpool play method to play the sound.

There are a few points to note when you actually use Soundpool to play a sound: soundpool Although you can load multiple sounds at once, you should avoid using Soundpool to play songs or play background music because of memory limitations, only those short, Dense sound is considered for playback using Soundpool.

Although Soundpool is more efficient than MediaPlayer, it is not absolutely non-existent, especially in mobile phones where performance is not good, and soundpool latency is even worse.

Play sound effects with 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.