Android使用SoundPool播放音效

作者:dsd2333 时间:2021-11-24 02:32:36 

本文实例为大家分享了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.  运行效果图:

Android使用SoundPool播放音效

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播放音效

来源:https://blog.csdn.net/m0_37868230/article/details/80764067

标签:Android,SoundPool,播放
0
投稿

猜你喜欢

  • C#函数式程序设计之用闭包封装数据的实现代码

    2021-06-30 01:44:34
  • Mybatis实现数据的增删改查实例(CRUD)

    2022-05-29 07:03:50
  • Maven实战之搭建Maven私服和镜像的方法(图文)

    2023-11-27 22:27:06
  • Java多线程-线程的同步与锁的问题

    2023-11-29 01:40:12
  • Java Lambda 表达式源码解析

    2021-06-30 08:12:48
  • Mybatis日志参数快速替换占位符工具的详细步骤

    2023-09-30 21:29:36
  • 如何解决Java多线程死锁问题

    2022-08-11 15:51:02
  • C#实现排列组合算法完整实例

    2023-04-16 09:36:59
  • java实现将域名解析成ip示例

    2021-11-20 10:33:33
  • SpringBoot项目打成war和jar的区别说明

    2023-10-05 04:54:15
  • Android使用ContentProvider实现查看系统短信功能

    2023-12-16 23:55:46
  • Android画画板的制作方法

    2022-08-25 06:08:07
  • 浅谈Glide缓存key的问题

    2023-10-05 05:26:35
  • 一文搞懂Java设计模式之责任链模式

    2021-12-24 22:19:40
  • Android 通过productFlavors实现多渠道打包方法示例

    2022-08-27 16:04:35
  • java开发之内部类的用法

    2023-02-04 21:30:07
  • Java实现的模糊匹配某文件夹下的文件并删除功能示例

    2022-02-28 13:51:46
  • Java详细分析LCN框架分布式事务

    2022-10-17 15:49:08
  • struts2 validation.xml 验证规则代码解析

    2021-09-14 22:01:27
  • Unity 按钮事件封装操作(EventTriggerListener)

    2022-07-08 10:07:08
  • asp之家 软件编程 m.aspxhome.com