Soundpool Sound Management

Source: Internet
Author: User

Soundpool (Android.media.SoundPool), as the name implies is the meaning of the sound pool, mainly used to play some short audio clips, support from the program's resources or file system loading. Compared with MediaPlayer, the Soundpool has the advantage of low CPU resource consumption and small reaction delay. In addition, Soundpool supports setting the sound quality, volume, playback ratio and other parameters to support the management of multiple audio streams by ID.

Soundpool Existing defects
1.SoundPool can only request 1M of memory space, which means we can only use a few short sound clips, rather than use it to play songs or play background music.
The 2.SoundPool provides pause and stop methods, but these methods are best advised not to be used easily, because sometimes they may cause your program to end somehow. It is recommended to do as much testing as possible when using these two methods, and some friends reflect that they do not immediately stop playing the sound, but instead of playing back the data in the buffer will stop, perhaps play more than a second.
3.SoundPool efficiency issues. In fact, the efficiency of Soundpool in these playback class is very good, but some friends in the G1 test it still has a delay of about 100ms, which may affect the user experience. Perhaps this cannot control soundpool itself, because this delay can be acceptable to a better-performing droid.
At this stage Soundpool has these shortcomings, but also has its irreplaceable advantages, based on these we recommend the use of soundpool:1 in the following cases. Sound effects (key tones, messages, etc.) in the application 2. Dense, short-lived sounds in the game (such as multiple ships exploding at the same time)

Import Android.media.AudioManager;
Import Android.media.SoundPool;

Soundpool soundpool;//Declaration Soundpool Object


Soundpool instantiation, the first parameter is the number of sounds that soundpool can support, which determines how much of the buffer the android has for it, the second parameter is the sound type, which is identified here as the system sound, in addition to the AUDIOMANAGER.STREAM_ Ring and Audiomanager.stream_music, the system will be based on different sounds for its different priority and buffer, the final parameter for sound quality, the higher the quality, sound effect is better, but consumes more system resources.

Soundpool= new Soundpool (10,audiomanager.stream_system,5);

The system loads the sound for Soundpool the first parameter is the context parameter, the second parameter is the ID of the sound, the third parameter is the priority of the sound, and when multiple sound conflicts cannot be played simultaneously, the system prioritizes the high priority. Generally we keep the sound information in the Res raw folder
Load the audio stream and return the ID in the pool
int SourceID = Soundpool.load (this,r.raw.collide,1);

Playback, the first parameter is id,id that is put into the soundpool order, such as now Collide.wav is the first, so its ID is 1. The second and third parameters are the volume control of the left and right channels. The fourth parameter is the priority, because only this one sound, so the priority is not important here. The fifth parameter is whether the loop plays, 0 is not looped, and 1 is a loop. The last parameter is the playback ratio, from 0.5 to 2, which is typically 1, which indicates normal playback.
Soundpool.play (1, 1, 1, 0, 0, 1);


Attention:
If Soundpool has just finished loading the load function, calling Soundpool's play function directly may appear
Error "Sample 1 not Ready"
Therefore, it is recommended that the call to load the resource function after load, the implementation of the resources to load the end of the listener function, after the monitoring of the resource loading end, play audio files.
Such as:
Soundpool Soundpool = new Soundpool (Ten, Audiomanager.stream_music, 5);
Load the audio stream and return the ID in the pool
Final int sourceid = Soundpool.load (mcontext, r.raw.move_sound, 0);
Play audio, the second parameter is the left channel volume, the third parameter is the right channel volume, the fourth parameter is the priority, the fifth parameter is the number of cycles, 0 does not loop, 1 cycles, the sixth parameter is the rate, the lowest rate is 0.5 up to 2, and 1 is the normal speed.
Soundpool.setonloadcompletelistener (New Onloadcompletelistener () {

public void Onloadcomplete (Soundpool soundpool, int sampleid, int status) {
TODO auto-generated Method Stub
Soundpool.play (SourceID, 2, 2, 0, 0, 1);
}
});

Soundpool Sound Management

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.