Android使用SoundPool实现播放音效

时间:2022-07-27
本文章向大家介绍Android使用SoundPool实现播放音效,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

如果在程序应用中(比如:游戏的音效等)需要播放密集、短促的音效,这时就使用SoundPool来播放音效,SoundPool使用音效池的概念来管理多个短促的音效,例如它可以开始就10个音效,以后在程序中按音效的ID进行播放。

SoundPool主要用于播放一些较短的声音片段,与MediaPlayer相比,SoundPool的优势在 于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、播放比率等参数。

一般使用SoundPool播放声音的步骤如下:

Step1:调用SoundPool.Builder的构造器创建SoundPool.Builder对象,并可通过该Builder对象为SoundPool设置属性; Step2:调用SoundPool的构造器创建SoundPool对象; Step3:调用SoundPool对象的load()方法从指定资源、文件中加载声音。最好使用HashMap< Integer, Integer 来管理所加载的声音; Step4:调用SoundPool的play()方法播放声音。

下面的Demo程序示范了如何使用SoundPool来播放音效,该程序提供三个按钮,分别用于播放不同的声音。

layout/activity_main.xml界面代码如下:

<?xml version="1.0" encoding="utf-8"? 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" 

  <Button
    android:id="@+id/bomb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="爆炸声" / 

  <Button
    android:id="@+id/shot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="射击声" / 

  <Button
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="射箭声" / 
</LinearLayout 

MainActivity.java逻辑代码如下:

package com.fukaimei.soundpooltest;

import android.media.AudioAttributes;
import android.media.SoundPool;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  Button bomb, shot, arrow;
  // 定义一个SoundPool
  SoundPool soundPool;
  HashMap<Integer, Integer  soundMap = new HashMap< ();

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bomb = (Button) findViewById(R.id.bomb);
    shot = (Button) findViewById(R.id.shot);
    arrow = (Button) findViewById(R.id.arrow);
    AudioAttributes attr = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME) // 设置音效使用场景
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build(); // 设置音效的类型
    soundPool = new SoundPool.Builder().setAudioAttributes(attr) // 设置音效池的属性
        .setMaxStreams(10) // 设置最多可容纳10个音频流
        .build(); // ①
    // load方法加载指定音频文件,并返回所加载的音效ID
    // 此处使用HashMap来管理这些音频流
    soundMap.put(1, soundPool.load(this, R.raw.bomb, 1)); // ②
    soundMap.put(2, soundPool.load(this, R.raw.shot, 1)); // ②
    soundMap.put(3, soundPool.load(this, R.raw.arrow, 1)); // ②
    bomb.setOnClickListener(this);
    shot.setOnClickListener(this);
    arrow.setOnClickListener(this);
  }

  // 重写OnClickListener监听器接口的方法
  @Override
  public void onClick(View v) {
    // 判断哪个按钮被单击
    if (v.getId() == R.id.bomb) {
      soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1); // ③
    } else if (v.getId() == R.id.shot) {
      soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1); // ③
    } else if (v.getId() == R.id.arrow) {
      soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1); // ③
    }
  }
}

上面Demo程序代码中标①的代码用于创建SoundPool对象;标②的代码用于使用SoundPool加载多个不同的声音;标③的代码则用于根据声音ID来播放指定的声音。这就是使用SoundPool播放声音的标准过程。

实际使用SoundPool播放声音时有如下几点需要注意:SoundPool虽然可以一次性加载多个声音,但由于内存限制,因此应该避免使用SoundPool来播放歌曲,只有那些短促、密集的声音才考虑使用SoundPool进行播放。

Demo程序运行效果界面截图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助。