Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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()方法从指定资源、@L_262_8@中加载声音。最好使用HashMap< Integer,Integer>来管理所加载的声音;
Step4:调用SoundPool的play()方法播放声音。

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_220_11845@as.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>
@H_86_0@mainActivity.java逻辑代码如下:

package com.fuKaiR_220_11845@ei.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方法加载指定音频@L_262_8@,并返回所加载的音效ID
    // 此处使用HashMap来管理这些音频流
    soundMap.put(1,soundPool.load(this,R.raw.bomb,1)); // ②
    soundMap.put(2,R.raw.shot,1)); // ②
    soundMap.put(3,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); // ③
    } else if (v.getId() == R.id.shot) {
      soundPool.play(soundMap.get(2),1); // ③
    } else if (v.getId() == R.id.arrow) {
      soundPool.play(soundMap.get(3),1); // ③
    }
  }
}

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

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

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

Android使用SoundPool实现播放音效


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

@H_502_35@

大佬总结

以上是大佬教程为你收集整理的Android使用SoundPool实现播放音效全部内容,希望文章能够帮你解决Android使用SoundPool实现播放音效所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: