Android使用SoundPool播放音效

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

本文实例为大家分享了Android使用SoundPool播放音效的具体代码,供大家参考,具体内容如下

SoundPool(int maxStreams, int streamType, int srcQuality) 参数依次是:

①指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量。
②指定声音类型,流类型可以分为STREAM_VOICE_CALL(通话), STREAM_SYSTEM(系统), STREAM_RING(铃声),STREAM_MUSIC(媒体音量) 和STREAM_ALARM(警报)四种类型。在AudioManager中定义。
③指定声音品质(采样率变换质量),一般直接设置为0!、

以下是对它的常用方法的介绍:

1.加载声音资源

load(Context context,int resid,int priority)
load(String path,int priority)
load(FileDescriptor fd,long offset,long length,int priority)
load(AssetFileDescriptor afd,int priority)

参数介绍:

  • context:上下文
  • resId:资源id
  • priority:没什么用的一个参数,建议设置为1,保持和未来的兼容性
  • path:文件路径
  • FileDescriptor:貌似是流吧,这个我也不知道
  • AssetFileDescriptor:从asset目录读取某个资源文件,其用法:AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3");

2.播放控制

play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)

参数依次是:

  • soundID:Load()返回的声音ID号
  • leftVolume:左声道音量设置
  • rightVolume:右声道音量设置
  • priority:指定播放声音的优先级,数值越高,优先级越大。
  • loop:指定是否循环:-1表示无限循环,0表示不循环,其他值表示要重复播放的次数
  • rate:指定播放速率:1.0的播放率可以使声音按照其原始频率,而2.0的播放速率,可以使声音按照其 原始频率的两倍播放。如果为0.5的播放率,则播放速率是原始频率的一半。播放速率的取值范围是0.5至2.0。

3.资源释放

方法:可以通过release()方法释放所有SoundPool对象所占据的内存和资源,也可以根据声音ID来释放。

下面是使用SoundPool实现的一个代码示例:

1.  运行效果图:

2.  MainActivity代码:

import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

 private Button btnOne;
 private Button btnTwo;
 private Button btnThree;
 private Button btnFour;
 private Button btnFive;
 private Button btn_release;
 private AssetManager aManager;
 private SoundPool mSoundPool = null;
 private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  aManager = getAssets();
  try {
   initSP();
  } catch (Exception e) {
   e.printStackTrace();
  }
  bindViews();
 }

 private void bindViews() {
  btnOne = (Button) findViewById(R.id.btn_play1);
  btnTwo = (Button) findViewById(R.id.btn_play2);
  btnThree = (Button) findViewById(R.id.btn_play3);
  btnFour = (Button) findViewById(R.id.btn_play4);
  btnFive = (Button) findViewById(R.id.btn_play5);
  btn_release = (Button) findViewById(R.id.btn_release);

  btnOne.setOnClickListener(this);
  btnTwo.setOnClickListener(this);
  btnThree.setOnClickListener(this);
  btnFour.setOnClickListener(this);
  btnFive.setOnClickListener(this);
  btn_release.setOnClickListener(this);

 }

 private void initSP() throws Exception{
  //设置最多可容纳5个音频流,音频的品质为5
  mSoundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 5);
  soundID.put(1, mSoundPool.load(this, R.raw.duang, 1));
  soundID.put(2 , mSoundPool.load(getAssets().openFd("biaobiao.mp3") , 1)); //需要捕获IO异常
  soundID.put(3, mSoundPool.load(this, R.raw.duang, 1));
  soundID.put(4, mSoundPool.load(this, R.raw.duang, 1));
  soundID.put(5, mSoundPool.load(this, R.raw.duang, 1));
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.btn_play1:
    mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
    break;
   case R.id.btn_play2:
    mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
    break;
   case R.id.btn_play3:
    mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
    break;
   case R.id.btn_play4:
    mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
    break;
   case R.id.btn_play5:
    mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1);
    break;
   case R.id.btn_release:
    mSoundPool.release(); //回收SoundPool资源
    break;
  }
 }
}

3.  activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">


 <Button
  android:id="@+id/btn_play1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="声音1" />


 <Button
  android:id="@+id/btn_play2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="声音2" />

 <Button
  android:id="@+id/btn_play3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="声音3" />

 <Button
  android:id="@+id/btn_play4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="声音4" />

 <Button
  android:id="@+id/btn_play5"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="声音5" />

 <Button
  android:id="@+id/btn_release"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="释放SoundPool" />

</LinearLayout>

点击声音1~5按钮会发出声音,但当点击最后一个release按钮将SoundPool释放后,再去按就没有任何效果了哦。

源码下载:Android使用SoundPool播放音效

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

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

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多