Use of SoundPool in Android

Source: Internet
Author: User

Use of SoundPool in Android

We all know that MediaPlayer uses a lot of resources and cannot play multiple audios at the same time. Therefore, we have a SoundPool, such as our common key tone or mobile phone prompt sound, for example, we have a lot of sound effects in game development. Below we will introduce her usage:

The procedure is as follows:

1. Create a SoundPool object

The source code is as follows:

/*** Method body in the SoundPool source code ** @ param maxStreams: Maximum number of audios x @ param streamType specifies the sound type, specify the * @ param srcQuality through the constant provided by the AudioManager class to specify the audio quality. The default value is 0 * @ return a SoundPool object, or null if creation failed */public SoundPool (int maxStreams, int streamType, int srcQuality)
2. Load the audio to be played:
/**     * @param context the application context     * @param resId the resource ID     * @param priority the priority of the sound. Currently has no effect. Use     *                 a value of 1 for future compatibility.     * @return a sound ID. This value can be used to play or unload the sound.     */ public int load(Context context, int resId, int priority);
3. play audio
/*** Play a sound from a sound ID. * @ param soundID: the audio returned by the load method * @ param leftVolume left-channel volume * @ param rightVolume right-channel volume * @ param priority. The greater the value, higher priority * @ param loop cycle times: 0 indicates no loop,-1 indicates a loop * @ param rate specifies the rate, the normal bit 1, the status is 0.5, highest 2 * @ return non-zero streamID if successful, zero if failed */public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate );
4. The case is as follows:

(1) Layout file:


  
  
   
   
   
  

(2) MainActivity. java File

Package com. mingrisoft; import java. util. hashMap; import android. app. activity; import android. media. audioManager; import android. media. soundPool; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {private SoundPool soundpool; // declare a SoundPool object // use HashMap to manage various audio private hashmaps
  
   
Soundmap = new HashMap
   
    
(); // Create a HashMap object @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button chimes = (Button) findViewById (R. id. button1); // get the "Wind ringtone" Button enter = (Button) findViewById (R. id. button2); // obtain the "cuckoo cry" Button notify = (Button) findViewById (R. id. button3); // get the "ringtone" Button ringout = (Button) findViewById (R. id. button4); // get the "Phone Sound" button soundpool = new SoundPool (5, AudioManager. STREAM_SYSTEM, 0); // create a SoundPool object, which can accommodate 5 audio streams // Save the audio streams to the HashMap object soundmap. put (1, soundpool. load (this, R. raw. chimes, 1); soundmap. put (2, soundpool. load (this, R. raw. enter, 1); soundmap. put (3, soundpool. load (this, R. raw. notify, 1); soundmap. put (4, soundpool. load (this, R. raw. ringout, 1); soundmap. put (5, soundpool. load (this, R. raw. ding, 1); // Add and click the event listener chimes for each button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {soundpool. play (soundmap. get (1), 1, 1, 0, 0, 1); // play the specified audio}); enter. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {soundpool. play (soundmap. get (2), 1, 1, 0, 0, 1); // play the specified audio}); y. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {soundpool. play (soundmap. get (3), 1, 1, 0, 0, 1); // play the specified audio}); ringout. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {soundpool. play (soundmap. get (4), 1, 1, 0, 0, 1); // play the specified audio soundpool. play (soundpool. load (MainActivity. this, R. raw. optional Y, 1), 1, 1, 0, 0, 1) ;}} // the event where the rewrite key is pressed @ Override public boolean onKeyDown (int keyCode, keyEvent event) {soundpool. play (soundmap. get (5), 1, 1, 0, 0, 1); // return true for the playback key ;}}
   
  

In addition, there are several small ringtones in the raw file of the resource file, which are not displayed here.

 

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.