当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用SoundPool实现播放音效

Android使用SoundPool实现播放音效

2020年03月09日  | 移动技术网IT编程  | 我要评论

天书奇梦,徐鹤宁最新演讲视频,贵阳市租房网

如果在程序应用中(比如:游戏的音效等)需要播放密集、短促的音效,这时就使用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程序运行效果界面截图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网