Android中SoundPool的使用步骤实例

所属分类: 软件编程 / Android 阅读数: 54
收藏 0 赞 0 分享

大家知道MediaPlayer占用的资源比较多,且不可以同时支持播放多个音频,所以我们有一种叫做SoundPool,比如我们常见的按键音或者是手机提示音,还比如我们在游戏的开发中会有大量的音效效果等,下边介绍一下她的用法:

步骤如下:

1.创建SoundPool对象

源码如下

 /**
   *SoundPool源码中的构造方法方法体
   * @param maxStreams 最多可以容纳多少个音频
   * @param streamType 指定的声音类型,通过AudioManager类提供的常量进行指定
   * @param srcQuality 指定音频的质量,默认为0
   * @return a SoundPool object, or null if creation failed
   */
  public SoundPool(int maxStreams, int streamType, int srcQuality)

2.加载所需要播放的音频:

/**
   * @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 a sound from a sound ID.
   * @param soundID 通过load方法返回的音频
   * @param leftVolume 左声道的音量
   * @param rightVolume 右声道的音量 
   * @param priority 优先级,值越大,优先级越高
   * @param loop 循环的次数:0为不循环,-1为循环
   * @param rate 指定速率,正常位1,为地位0.5,最高位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.案例如下:

(1)布局文件:

<?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="风铃声" />
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="布谷鸟叫声" />
  <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="门铃声" />
  <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="电话声" />
</LinearLayout>

(2)MainActivity.java文件

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;  //声明一个SoundPool对象
  //使用HashMap管理各种音频
  private HashMap<Integer, Integer> soundmap = new HashMap<Integer, Integer>();  //创建一个HashMap对象
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button chimes = (Button) findViewById(R.id.button1);  //获取“风铃声”按钮
    Button enter = (Button) findViewById(R.id.button2);   //获取“布谷鸟叫声”按钮
    Button notify = (Button) findViewById(R.id.button3);  //获取“门铃声”按钮
    Button ringout = (Button) findViewById(R.id.button4);  //获取“电话声”按钮
    soundpool = new SoundPool(5,
        AudioManager.STREAM_SYSTEM, 0); //创建一个SoundPool对象,该对象可以容纳5个音频流
    //将要播放的音频流保存到HashMap对象中
    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));
    //为各按钮添加单击事件监听器
    chimes.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1); //播放指定的音频
      }
    });
    enter.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);//播放指定的音频
      }
    });
    notify.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);//播放指定的音频
      }
    });
    ringout.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);//播放指定的音频
        soundpool.play(soundpool.load(MainActivity.this, R.raw.notify, 1), 1, 1, 0, 0, 1);
      }
    });
  }
  //重写键被按下的事件
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);   //播放按键音
    return true;
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多