当前位置: 移动技术网 > IT编程>移动开发>Android > Android中SoundPool的使用步骤实例

Android中SoundPool的使用步骤实例

2019年07月23日  | 移动技术网IT编程  | 我要评论

美竹凉子人体,神犬小巴迪国语,南京独居老人坠亡

大家知道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;
  }
}

总结

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

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

相关文章:

验证码:
移动技术网