Playing audio using SoundPool in Android

Source: Internet
Author: User

Playing audio using SoundPool in Android

Today, I learned how to set background audio for the application and found that through EncapsulationSoundPoolClass can do this well.


The SoundPool class is suitable for playing a comparison similar to game sound effects.Short promotionAndRelatively smallAnd supports playing multiple audio streams at the same time, while MediaPlayer is more suitable for playing large audio streams.

The following describes the basic usage of the SoundPool class:

1.Load the required audio resources using the load () method.

2.Use the setOnLoadCompleteListener () method of the created SoundPool object to create and pass in the SoundPool. OnLoadCompleteListener object. Reload the public void onLoadComplete (SoundPool arg0, int arg1, int arg2) method to check whether the audio is loaded successfully.

3.Play audio streams using the play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) method. soundID is the audio number to be played, priority is the audio playback priority, loop is the number of playback times (-1 is infinite loop playback), rate is the playback rate ~


The following is a class of playing audio encapsulated when redo FlappyBird. Compared with MediaPlayer, this method is more efficient:

Package com. example. flappy. util; import java. util. hashMap; import android. media. audioManager; import android. media. soundPool; import android. widget. toast; import com. example. flappy. mainActivity; import com. example. flappy. r;/** SoundPlayer playing audio */public class SoundPlayer {private SoundPool soundPool; private MainActivity mainActivity; private HashMap
 
  
Map; public SoundPlayer (MainActivity mainActivity) {this. mainActivity = mainActivity; this. map = new HashMap
  
   
(); // Number of three parameters of the SoundPool constructor: // 1. maximum number of streams simultaneously played (Yes simultaneously) // 2. stream type // 3. conversion quality this. soundPool = new SoundPool (8, AudioManager. STREAM_MUSIC, 0); this. soundPool. setOnLoadCompleteListener (new SoundPool. onLoadCompleteListener () {@ Overridepublic void onLoadComplete (SoundPool arg0, int arg1, int arg2) {// triggered when audio loading fails // remember that audio loading may fail, must be processed here});} public void initSounds () {this. map. put (1, this. soundPool. load (this. mainActivity, R. raw. flappy, 1); this. map. put (2, this. soundPool. load (this. mainActivity, R. raw. pass, 1); this. map. put (3, this. soundPool. load (this. mainActivity, R. raw. hit, 1); this. map. put (4, this. soundPool. load (this. mainActivity, R. raw. die, 1); this. map. put (5, this. soundPool. load (this. mainActivity, R. raw. swooshing, 1 )); // load all the sounds used by your APP in this way} // import the first and second audio records to be played to the public void playSound (int sound, int loop) {this. soundPool. play (sound, 1, 1, 1, loop, 1.0f);} public void release () {this. soundPool. release ();}}
  
 



Reprinted please indicate the source: http://blog.csdn.net/gophers




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.