Summary of soundpool in Android

Source: Internet
Author: User

I only know that mediaplayer can be used to play music in Android.
Soundpool can be used, and soundpool can be used to play a few short sounds with high response speed requirements,
For example, in the game, the burst sound, and mediaplayer is suitable for playing long points.
1. The soundpool uses an independent thread to load music files and does not block operations in the main UI thread. However, if the audio file is too large to be loaded, it may cause serious consequences when we call the play method. Here, the android SDK provides a soundpool. the onloadcompletelistener class helps us understand whether a media file is loaded. We can obtain it by reloading the onloadcomplete (soundpool, int sampleid, int status) method.
2. from the above onloadcomplete method, we can see that this class has many parameters, such as ID. Yes, soundpool can process multiple media initialization at a time during load and put it into the memory, which is much more efficient than mediaplayer. Renren
3. The soundpool class supports playing multiple sound effects at the same time, which is necessary for the game, while the mediaplayer class supports playing only one file and one file simultaneously.

Usage:
1. Create a soundpool

Public soundpool (INT maxstream, int streamtype, int srcquality)

Maxstream -- maximum number of streams simultaneously played

Streamtype -- stream type, which is generally stream_music (listed in the audiomanager class)

Srcquality -- sampling rate conversion quality. No effect currently. Use 0 as the default value.

Eg.

Soundpool = new soundpool (3, audiomanager. stream_music, 0 );

Creates a soundpool that supports simultaneous playback of up to three streams and marks the type as music.

2. Put multiple voices in hashmap, for example
Soundpool = new soundpool (4, audiomanager. stream_music, 100 );
Soundpoolmap = new hashmap <integer, integer> ();
Soundpoolmap. Put (1, soundpool. Load (this, R. Raw. Dingdong, 1 ));

Soundpool loading: Women's brand rankings
Int load (context, int resid, int priority) // load data from the APK Resource
Int load (filedescriptor FD, long offset, long length, int priority) // load data from the filedescriptor object
Int load (assetfiledescriptor AFD, int priority) // load data from the asset object
Int load (string path, int priority) // load data from the complete file path
The last parameter is the priority.

3 play
play (INT soundid, float leftvolume, float rightvolume, int priority, int loop, float rate), where leftvolume and rightvolume indicate the Left and Right volume, priority indicates the priority, loop indicates the number of cycles, and rate indicates the rate. For example,
// The minimum rate is 0.5, and the maximum rate is, which indicates the normal speed
sp. play (soundid, 1, 1, 0, 0, 1);
you can use the pause (INT streamid) method to stop a stream, here, both streamid and soundid specify the total number in the first parameter for constructing the soundpool class, and Id starts from 0. Risk situation Java-JavaScript

Related Article

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.