Android's Soundpool

Source: Internet
Author: User

Development Android software We may often need to play multimedia sound files, generally use the MediaPlayer class but this class occupies a lot of resources, for games and other applications may not be very suitable, The Soundpool class Android.media.SoundPool in the SDK , as the name implies, is the meaning of the sound pool. Mainly plays some short sound clips, which can be loaded from the program's resources or file system, with fewer CPU resources and shorter reaction latencies than the MediaPlayer class.

           Soundpool , compared to other sound playback classes, is characterized by the ability to set your own sound quality, volume, Playback ratios, and other parameters. And it can manage multiple audio streams at the same time, each with its own ID and the management of an audio stream through ID .

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

               public Soundpool (int Maxstream, int streamtype, int srcquality)  
                maxstream--The maximum number of streams that are simultaneously played  
                   streamtype--stream type, typically Stream_music (listed in 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.
2. Load the audio stream from a resource or file: Load (context context, int resId, int priority);

               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.

                         generally put a number of sounds into the hashmap, such as  
                          Soundpool = new Soundpool (4, Audiomanager.stream _music,  
                          soundpoolmap = new Hashmap<integer, integer> ();   & nbsp
                         Soundpoolmap.put (1, Soundpoo L.load (This, R.raw.dingdong, 1));
        3,   play sound play (int soundid, float leftvolume, float rightvolume, int pr iority, int loop, float rate);

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

where Leftvolume and Rightvolume represent the volume around,

Priority is prioritized,

Loop indicates the number of loops,

rate, e.g.//rate minimum 0.5 up to 2, 1 = 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 construct the Soundpool class.

The total quantity is indicated in the first parameter, and the ID starts at 0.

1. Soundpool can only request 1M of memory space, which means we can only use a few short sound clips, rather than using it to play songs or play background music.

2. Soundpool provides pause and stop methods, but these methods are not recommended for easy use, because sometimes they may cause your program to end somehow. The Android Development Network recommends using both methods to do as much testing as possible, and some friends reflect that they do not immediately stop playing sound, but instead of playing back the data in the buffer will stop, and may play more than a second.

3. Soundpool efficiency issues. In fact, the efficiency of Soundpool in these playback classes is very good, which may affect the user experience. Perhaps this cannot control Soundpool itself, because this delay can be acceptable to a better-performing Droid .

Android's Soundpool

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.