Android uses SoundPool to play sound effects, and androidsoundpool

Source: Internet
Author: User

Android uses SoundPool to play sound effects, and androidsoundpool

In Android development, we often use MediaPlayer to play audio files. However, MediaPlayer has some shortcomings, such as high resource usage, long latency, and not support simultaneous playback of multiple audios. These disadvantages determine that MediaPlayer is not ideal in some scenarios, for example, in game development with relatively high time precision.

URL: http://www.cnblogs.com/wuyudong/p/5679191.html.

In game development, we often need to play some game sound effects (such as bullet explosion and object impact). These sound effects are characterized by short promotion, intensive, and low latency. In this scenario, we can use SoundPool instead of MediaPlayer to play these sound effects.

SoundPool (android. media. SoundPool) is called the sound pool. It is mainly used to play short sound clips and supports loading from program resources or file systems. Compared with MediaPlayer, SoundPool has the advantage of low CPU usage and low response latency. In addition, SoundPool also allows you to set parameters such as sound quality, volume, and playback ratio, and allows you to manage multiple audio streams by ID.

As we know, SoundPool has some Design Bugs. Some bugs have not been fixed since the firmware version 1.0. We should be careful when using them. I believe Google will fix these problems in the future, but we 'd better list them as follows:
1. SoundPool can only apply for a maximum of 1 MB of memory space, which means that we can only use some very short sound clips instead of playing songs or playing background music.

2. SoundPool provides pause and stop methods, but it is recommended that you do not use these methods easily, because sometimes they may cause your program to terminate inexplicably. We recommend that you do as much test work as possible when using these two methods. Some friends report that they do not stop playing the sound immediately, but stop playing the data in the buffer zone, it may take a second to play.

3. SoundPool efficiency issues. In fact, SoundPool efficiency is good in these playing classes, but some friends test it in G1 with a latency of about ms, which may affect the user experience. Maybe this does not care about SoundPool itself, because the latency in Droid with better performance is acceptable.

SoundPool has these defects at present, but it also has an irreplaceable advantage. Based on these, we recommend that you use SoundPool in the following scenarios: 1. audio Effect in the application (key tone, message, etc.) 2. intensive and transient sounds in the game (such as the simultaneous explosion of multiple ships)

Next, let's take a look:

Create a project and design the Layout

<RelativeLayout xmlns: 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 "> <Button android: onClick =" click "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_centerHorizontal =" true "android: layout_centerVertical = "true" android: text = ""/> </RelativeLayout>

Create a raw folder in the res folder and import audio files.

Add code:

Package com. wuyudong. soundpool; import android. media. audioManager; import android. media. soundPool; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; public class MainActivity extends Activity {private SoundPool soundPool; private int soundId; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); soundPool = new SoundPool (5, AudioManager. STREAM_MUSIC, 0); // load the sound resource soundId = soundPool. load (this, R. raw. alert, 1);} public void click (View view) {/** int android. media. soundPool. 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 * priority (0 = lowest priority) loop mode (0 = no loop,-1 = loop * forever) rate playback rate (1.0 = normal playback, range 0.5 to 2.0) * Returns: non-zero streamID if successful, zero if failed */soundPool. play (soundId, 1.0f, 1.0f, 0, 0, 1.0f );}}

 

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.