Examples of using SoundPool in Android

  • 2021-11-02 02:45:08
  • OfStack

As we all know, MediaPlayer takes up a lot of resources and cannot support playing multiple audio at the same time, so we have one called SoundPool, such as our common key tone or mobile phone prompt tone, and for example, we will have a lot of sound effects in the development of games. Here is an introduction to her usage:

The steps are as follows:

1. Create an SoundPool object

The source code is as follows


 /**
   *SoundPool Construct method body in source code 
   * @param maxStreams  How many audio can you hold at most 
   * @param streamType  Specifies the sound type specified by the AudioManager Specifies the constants provided by the class 
   * @param srcQuality  Specifies the quality of audio, which defaults to 0
   * @return a SoundPool object, or null if creation failed
   */
  public SoundPool(int maxStreams, int streamType, int srcQuality)

2. Load the audio you want to play:


/**
   * @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);

Step 3 Play audio


 /**
   * Play a sound from a sound ID.
   * @param soundID  Pass load The audio returned by the 
   * @param leftVolume  Volume of left channel 
   * @param rightVolume  Volume of right channel  
   * @param priority  Priority, the higher the value, the higher the priority 
   * @param loop  Number of cycles: 0 To not loop, -1 Is a loop 
   * @param rate  Specify rate, normal bit 1 For status 0.5 , highest position 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. Examples are as follows:

(1) Layout file:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Wind bells " />
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Cuckoo calls " />
  <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Doorbell " />
  <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Telephone sound " />
</LinearLayout>

(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;  // Declaration 1 A SoundPool Object 
  // Use HashMap Manage various audio 
  private HashMap<Integer, Integer> soundmap = new HashMap<Integer, Integer>();  // Create 1 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 bell" button 
    Button enter = (Button) findViewById(R.id.button2);   // Get the cuckoo call button 
    Button notify = (Button) findViewById(R.id.button3);  // Get the doorbell button 
    Button ringout = (Button) findViewById(R.id.button4);  // Get the Phone Sound button 
    soundpool = new SoundPool(5,
        AudioManager.STREAM_SYSTEM, 0); // Create 1 A SoundPool Object that can hold 5 Audio streams 
    // Save the audio stream you want to play 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 a click event listener for each button 
    chimes.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1); // Plays the specified audio 
      }
    });
    enter.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);// Plays the specified audio 
      }
    });
    notify.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);// Plays the specified audio 
      }
    });
    ringout.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);// Plays the specified audio 
        soundpool.play(soundpool.load(MainActivity.this, R.raw.notify, 1), 1, 1, 0, 0, 1);
      }
    });
  }
  // Override the event that the key is pressed 
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);   // Play key tone 
    return true;
  }
}

Summarize


Related articles: