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. 运行效果图:
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


猜你喜欢
- 最近参与了开发一款旅行APP,其中包含实时聊天和动态评论功能,终于耗时几个月几个伙伴完成了,今天就小结一下至于实时聊天功能如果用户不多的情况
- jdk中自带了很多工具可以用于性能分析,位于jdk的bin目录下,jvisualvm工具可以以图形化的方式更加直观的监控本地以及远程的jav
- 1.通过看logcat下的日志2.通过adb命令3.通过写代码获取3.1写一个工具类打印系统时间3.2 在Application启动的时候打
- 目录Mybatis简介Mybatis开发步骤:Mybatis的映射文件概述Mybatis的增删改查操作MyBatis的核心配置文件概述MyB
- 场景重现:1.微信小程序向后台发送请求 ——而后台web采用的springSecuriry没有token生成,就会拦截请求,,所以小编记录下
- C++中一个重要的特性就是指针,指针不仅具有获得地址的能力,还具有操作地址的能力。指针可以用于数组、或作为函数的参数,用来访问内存和对内存的
- 1、Json的制作package com.example.usingjson2; import org.json.
- 1.内部类概念及分类将一个类定义在另一个类的内部或者接口内部或者方法体内部,这个类就被称为内部类,我们不妨将内部类所在的类称为外围类,除了定
- 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。本算法利用62个可打印字符,通过随机
- Android 完全退出的实例详解首先,在基类BaseActivity里,注册RxBus监听:public class BaseActivi
- 不得已重新配置,这里记下详细步骤,分享给大家。一、安装jdk,具体步骤如下:1、将jdk-7u4-linux-i586.tar拷贝到linu
- Java线程分为两类分别为daemon线程(守护线程)和User线程(用户线程),在JVM启动时候会调用main函数,main函数所在的线程
- 我在做毕设的时候采用shiro进行登录认证和权限管理的实现。其中需求涉及使用三个角色分别是:学生、教师、管理员。现在要三者实现分开登录。即需
- 本文实例为大家分享了Java Swing实现扫雷源码的具体代码,供大家参考,具体内容如下先来看下效果运行时只需要创建一个GameWindow
- 首先给出代码和输出://import java.sql.DriverManager;//import java.sql.SQLExcepti
- 本文为大家分享了使用entrySet方法获取Map集合中元素的具体代码,供大家参考,具体内容如下/*--------------------
- 这篇文章主要介绍了springboot配置aop切面日志打印过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 在混淆编译之前,我的程序可以正常运行,混淆编译时,报告如下错误: Error:Execution failed for task ‘:gvi
- 现在android的每一个项目都会需要设置为全屏,现在介绍两种设置为全屏的方式。一、在配置文件中设置android:theme=”@andr
- SpringCloud Zuul 是SpringCloud系列的网关实现,具有均衡负载,将非业务性校验剥离出来,使微服务专注于业务的一个组件