Soundpool Summary in Android

Source: Internet
Author: User

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

eg.

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.


21 like to put multiple sounds into the hashmap, such as
Soundpool = new Soundpool (4, Audiomanager.stream_music, +);
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.

3 plays
Play (int soundid, float leftvolume, float rightvolume, int priority, Int. loop, float rate), where leftvolume and Rightvolume represent Left and right volume, priority is prioritized, loop indicates the number of loops, rate represents
//Rate minimum 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

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.