Android uses Soundpool to play sound

Source: Internet
Author: User

In Android development we often use MediaPlayer to play audio files, but MediaPlayer there are some shortcomings, such as: high resource consumption, long delay time, does not support multiple audio simultaneous playback and so on. These shortcomings have determined that the use of MediaPlayer in certain situations will not be ideal, for example in game development where time accuracy is a relatively high requirement.

This article address: http://www.cnblogs.com/wuyudong/p/5679191.html, reprint please indicate source address.

In game development we often need to play some game sound effects (such as bullet explosion, object impact, etc.), the common characteristics of these sound effects are short, dense, less delay. In this scenario, we can use Soundpool instead of MediaPlayer to play these sounds.

Soundpool (Android.media.SoundPool), as the name implies is the meaning of the sound pool, mainly used to play some short audio clips, support from the program's resources or file system loading. Compared with MediaPlayer, the Soundpool has the advantage of low CPU resource consumption and small reaction delay. In addition, Soundpool supports setting the sound quality, volume, playback ratio and other parameters to support the management of multiple audio streams by ID.

As far as the information is known, Soundpool has some design bugs, some of which have not been repaired since firmware version 1.0, we should be careful when using. I believe Google will fix these problems in the future, but we'd better list them:
1. Soundpool can only request 1M of memory space, which means we can only use a few short sound clips, rather than using it to play songs or play background music.

2. Soundpool provides pause and stop methods, but these methods are not recommended for easy use, because sometimes they may cause your program to end somehow. It is recommended to do as much testing as possible when using these two methods, and some friends reflect that they do not immediately stop playing the sound, but instead of playing back the data in the buffer will stop, perhaps play more than a second.

3. Soundpool efficiency issues. In fact, the efficiency of Soundpool in these playback class is very good, but some friends in the G1 test it still has a delay of about 100ms, which may affect the user experience. Perhaps this cannot control soundpool itself, because this delay can be acceptable to a better-performing droid.

At this stage Soundpool has these shortcomings, but also has its irreplaceable advantages, based on these we recommend the use of soundpool:1 in the following cases. Sound effects (key tones, messages, etc.) in the application 2. Dense, short-lived sounds in the game (such as multiple ships exploding at the same time)

Next, Practice:

Create a new project, design a layout

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    <ButtonAndroid:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "First Aid" /></Relativelayout>

Create a new Raw folder under the Res folder, import the audio file

Add code:

 PackageCom.wuyudong.soundpool;ImportAndroid.media.AudioManager;ImportAndroid.media.SoundPool;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {PrivateSoundpool Soundpool; Private intSoundid; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Soundpool=NewSoundpool (5, Audiomanager.stream_music, 0); //Load Sound ResourcesSoundid = Soundpool.load ( This, R.raw.alert, 1); }     Public voidClick (View view) {/** int android.media.SoundPool.play (int soundid, float leftvolume, float rightvolume, * int priority, int loops, float rate) * Parameters:soundid a soundid returned by the LOA  D () * function Leftvolume Left volume value (range = 0.0 to 1.0) * Rightvolume Right volume value (range = 0.0 to 1.0) "priority Stream" (0 = Lowest priority) Loop loop mode (0 = no loop, 1 = loop * for) EVER) rate Playback rate (1.0 = Normal playback, range 0.5 to 2.0) * Returns:non-zero Streamid if successful, Zer o If failed*/Soundpool.play (Soundid,1.0f, 1.0f, 0, 0, 1.0f); }}

Android uses Soundpool to 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.