Previously only knew Android can use MediaPlayer to play music, originally only today discovered
can use the Soundpool, with Soundpool can sow some short reaction speed requires high sound,
such as the blasting sound in the game, and MediaPlayer suitable for playing long points.
1. Soundpool Loading music files uses a separate thread that does not block the operation of the UI main thread. But here if the sound file is too large to load, we may have serious consequences when we call the Play method, where Android The SDK provides a Soundpool.onloadcompletelistener class to help us understand whether the media files are loaded and finished, we reload Onloadcomplete (soundpool soundpool, int SampleID, int status) method can be obtained.
2. From the Onloadcomplete method above can be seen that the class has a number of parameters, such as ID, yes Soundpool can handle multiple media at load initialization and into memory, where efficiency is much higher than MediaPlayer.
3. The Soundpool class supports simultaneous playback of multiple sound effects, which is necessary for the game, while the MediaPlayer class is a single file that can be played synchronously.
How to use:
1. Create a Soundpool
Public Soundpool (int maxstream, int streamtype, int srcquality)
maxstream--the maximum number of simultaneous streams to be played
streamtype--type of stream, typically stream_music (specifically listed in the Audiomanager Class)
srcquality--sample rate conversion quality, currently no effect, use 0 as the default value
Soundpool Soundpool = new Soundpool (3, audiomanager.stream_music, 0);
Created a soundpool that supports simultaneous playback of up to 3 streams, with types marked as music.
2 generally put multiple sounds into the hashmap, such as
Soundpool = new Soundpool (4, Audiomanager.stream_music, 100);
Soundpoolmap = new Hashmap<integer, integer> ();
Soundpoolmap.put (1, Soundpool.load (this, R.raw.dingdong, 1));
Loading of Soundpool:
int load (context context, int resId, int priority)//load from APK resource
int load (filedescriptor fd, long offset, long length, int priority)//load from FileDescriptor object
int load (assetfiledescriptor afd, int priority)//load from Asset object
int load (String path, int priority)//loaded from full file path name
The last parameter is a priority.
Play (int soundid, float leftvolume, float rightvolume, int priority, Int. loop, float rate), where Leftvolume and Rightvolume represent the left and right volume Priority, loop indicates the number of loops, rate represents
Rate min. 0.5 up to 2, 1 for normal speed
Sp.play (Soundid, 1, 1, 0, 0, 1);
Instead of stopping, you can use the pause (int streamid) method, where both Streamid and Soundid indicate the total number in the first argument of the constructed Soundpool class, and the ID starts at 0.
Soundpool Summary in Android