Soundpool Sound in Android

Source: Internet
Author: User

It is ideal for playing sound that needs to be played repeatedly, but with shorter time. It supports simultaneous playback of multiple sounds, which are loaded into the list at the beginning of the system and can be called by the ID of these sounds.

1. Create a Soundpool object: New Soundpool (int maxstreams, int streamtype, int srcquality);

The first parameter is the number of sounds that soundpool can support, which determines how much of the buffer the Android opens for it, the second parameter is the sound type, The final parameter is sound quality, the higher the quality, the better the sound effect, but consumes more system resources.

2, load the audio stream from a resource or file:  

  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;
       iint  Load (String path, int Priority)  //load   from full file path name;
     
Priority Precedence

3. Play sound Play (int soundid, float leftvolume, float rightvolume, int priority, int loop, float rate)

The first argument is Id,id, which is the order in which it is put into soundpool, for example, 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.

The code is as follows:

Private Soundpool Soundpool;

Soundpool = new Soundpool (4, Audiomanager.stream_music, 5);
final int load = Soundpool.load (this, R.raw.wen, 1);
Soundpool.setonloadcompletelistener (New Onloadcompletelistener () {
public void Onloadcomplete (Soundpool soundpool, int sampleid, int status) {
Soundpool.play (Load, 1, 1, 0, 0, 1);
}
});

Note the point:

1. soundpool can only request 1M of memory space          Instead of using it to play songs or play background music.
  
,  Because sometimes they may cause your program to end somehow.
  
3. soundpool efficiency issues. In fact, the efficiency of Soundpool in these playback class is very good

Soundpool Sound 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.