Disadvantages of MediaPlayer:
High resource consumption and long delay time
Multiple sound effects are not supported at the same time
Soundpool is mainly used for playing some short sound clips, low CPU resource occupancy and reaction delay, but also support self-color set sound quality, volume, playback ratio and other parameters, avoid using Soundpool to play songs or play background music, Only those short, dense sounds consider using soundpool to play
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 would normally use Stream_musi C.
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 "the sound". currently has no effect. Use a value of 1 for the future compatibility.
public int Load (context context, int resId, int. priority)
Parameters
Context the application context
ResId the Resource ID
Priority the "the sound". currently has no effect. Use a value of 1 for the future compatibility.
public int Load (String path, int. priority)
Parameters
Path the path to the audio file
Priority the "the sound". currently has no effect. Use a value of 1 for the future compatibility.
public int Load (filedescriptor fd, long offset, long length, int priority)
Parameters
FD a FileDescriptor object
Offset offset to the start of the
Length length of the sound
Priority the "the sound". currently has no effect. Use a value of 1 for the future compatibility.
Play 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 loop Mode (0 = no loop, 1 = Loop forever)
Rate Playback Rate (1.0 = Normal playback, range 0.5 to 2.0)
to play a sound using Soundpool:
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, preferably using hashmap<integer,integer> to manage the loaded sound
Call Soundpool Play method to play 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;//define a soundpoolsoundpool soundpool; Hashmap<integer, integer> soundmap =new Hashmap<integer, integer> (); @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),//Set up to hold up to 10 audio streams, The audio quality is 5soundPool = new Soundpool (Ten, Audiomanager.stream_system, 5); The ①//Load method loads the specified audio file and returns the loaded audio ID. The HashMap is used here to manage these audio streams soundmap.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);} Override method for Onclicklistener listener interface @overridepublic void OnClick (View source) {//Determine which button was clicked on 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);