Android SoundPool playback sound effects

Source: Internet
Author: User

Disadvantages of MediaPlayer:

High resource occupation and long latency

Multiple sound effects cannot be played simultaneously.

SoundPoolIt is mainly used to play a few short audio clips, with low CPU resource usage and low response latency. It also supports setting parameters such as sound quality, volume, and playback ratio by color, do not use SoundPool to play songs or background music for games. Only short and intensive sounds are considered playing in SoundPool.

Constructor:

Public SoundPool (int maxStreams, int streamType, int srcQuality)

Parameters
MaxStreams the maximum number of simultaneous streams for this SoundPool object
StreamType the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.
SrcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.
Returns
A SoundPool object, or null if creation failed

Load sound:

Public int load (AssetFileDescriptor afd, int priority)

Parameters
Afd an asset file descriptor
Priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

Public int load (Context context, int resId, int priority)

Parameters
Context the application context
ResId the resource ID
Priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

Public int load (String path, int priority)

Parameters
Path the path to the audio file
Priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

Public int load (FileDescriptor fd, long offset, long length, int priority)

Parameters
Fd a FileDescriptor object
Offset to the start of the sound
Length of the sound
Priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

Playing sound:

Public final int 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)


To use SoundPool to play a sound, follow these steps:

Call the SoundPool constructor to create the SoundPool object.

Call the load () method of the SoundPool object to load the sound from the specified resource, file, it is best to use HashMap To manage the loaded sounds

Call the play method of SoundPool to play the sound.


Example program:

Import java. util. hashMap; import android. app. activity; import android. media. audioManager; import android. media. soundPool; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class SoundPoolTest extends Activity implements OnClickListener {Button bomb, shot, arrow; // defines a SoundPoolSoundPool soundPool; HashMap
 
  
SoundMap = new HashMap
  
   
(); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); bomb = (Button) findViewById (R. id. bomb); shot = (Button) findViewById (R. id. shot); arrow = (Button) findViewById (R. id. arrow); // you can set up to 10 audio streams. The audio quality is 5 soundPool = new SoundPool (10, AudioManager. STREAM_SYSTEM, 5); // ① // load the specified audio file and return the loaded audio ID. // Use HashMap to manage these audio streams. put (1, soundPool. load (this, R. raw. bomb, 1); // ② soundMap. put (2, soundPool. load (this, R. raw. shot, 1); soundMap. put (3, soundPool. load (this, R. raw. arrow, 1); bomb. setOnClickListener (this); shot. setOnClickListener (this); arrow. setOnClickListener (this);} // Method for rewriting the OnClickListener listener interface @ Overridepublic void onClick (View source) {// determine which button is clicked switch (source. getId () {case R. id. bomb: soundPool. play (soundMap. get (1), 1, 1, 0, 0, 1); // ③ break; case R. id. shot: soundPool. play (soundMap. get (2), 1, 1, 0, 0, 1); break; case R. id. arrow: soundPool. play (soundMap. get (3), 1, 1, 0, 0, 1); break ;}}}
  
 



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.