Today to do a small demo with the Soundpool, summed up.
MediaPlayer Disadvantage : can not play music overlap, packaging is relatively high, so loading will be slower.
Soundpool Introduction
The Android system provides another class for playing sound. Used to load multiple sound effects, short sounds and multiple short sounds, you can set your own sound quality, volume, repetition and priority. It is still very useful on certain occasions.
belongs to the Android.media package and inherits from object.
Public soundpool (int maxstreams, int streamtype, int srcquality){...} parameter one Maxstreams:How many sounds are supported (that is, the size of the pool)parameter two Streamtype: Specifies that the sound type gets the corresponding constant value by Audiomanager. parameter three srcquality: The sampling rate transforms the quality , does not use. The default is 0, no data found.
<span style= "FONT-SIZE:24PX;" >soundpool<span style= "color: #ff0000;" > pool</span> = new Soundpool (3, audiomanager.stream_music, 0);</span>
specifies a maximum of three audio for this pool, using the the audio stream used for the system sound. InAudiomanager parameters are many, can be poked---> Audiomanager.
There are four methods of construction:
- public int Load (String path, int. priority)
Load from full file path name
Path: File path
priority: Priorities,0 of the lowest priority level return value: The ID of this sound, used to turn music on and off.
- public int Load (assetfiledescriptorAfD, int. priority)
loading from a asset object AfD: A descriptor in a assets file that can obtain information about the file under assets; Priority : return value:
- public int Load (context context, int resId, int. priority)
get from APK resource
Context: resId: ID of file (file in res folder only) Priority :
- public int Load (filedescriptor fd, long offset, long length, int priority)
loading from a FileDescriptor object
fd: An instance of a file description classOffset : The amount of offsets from the sound file (from where to play)Length : soundPriority :
Construction Method:Public final int play (int soundid, float leftvolume, float rightvolume,int priority, int. loop, float rate) 5 >parameter One soundid: The sound ID that is played is returned by the Load method parameter two leftvolume: Left volume, value from (0.0-1.0) parameter three rightvolume: Right volume, value from (0.0-1.0) parameter four priority: precedence, 0 has the lowest priority. parameter five loop: loop, 0 not loop, 11 straight loop. parameter six rate: playback speed, 1 is the normal rate. range from 0.5-2.
If you need to play multiple songs: A collection is required to store the audio ID, which is the int returned by the Load method above. If you specify a song to play: You don't have to build the collection, assuming that the audio is in
<span style= "FONT-SIZE:24PX;" >//Get Resources </span>
<span style= "FONT-SIZE:24PX;" ><span style= "White-space:pre" ></span>assetfiledescriptor fd = Getresources (). OpenRawResourceFd ( R.raw.music) </span>
<span style= "FONT-SIZE:24PX;" >//Loading audio <span style= "White-space:pre" ></span>int soundid = Pool.load (FD, 1);</span>
<span style= "FONT-SIZE:24PX;" >//Play audio, Soundid is the above return value <span style= "White-space:pre" ></span>pool.play (Soundid, 1, 1, 0, 0, 1); </ Span>
This completes the playback of a piece of audio. One last question: Play multiple audio.
declaring a collection to hold audioHashmap<integer, integer> soundmap = new Hashmap<integer, integer> ();
soundmap.put (1, Pool.load (this, R.raw.music1, 1));
soundmap.put (2, Pool.load (this, R.raw.music2, 1));
soundmap.put (3, Pool.load (this, R.RAW.MUSIC3, 1));
storage of three audio;Pool.play (Soundmap.get (1), 1, 1, 0, 0, 1);
Pool.play (Soundmap.get (2), 1, 1, 0, 0, 1);
Pool.play (Soundmap.get (3), 1, 1, 0, 0, 1);
This completes the playback of multiple audio
- Freeing Resources and other
The release () method frees the memory and resources occupied by all Soundpool objects.
Pause (soundid) pauses playback
Stop (Soundid) stops playing
......
[Android] Soundpool detailed