android 播放一段音效,android 之使用SoundPool播放音效(Duang~)

1.相关方法介绍:

1)构造方法:

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

①指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量。

②指定声音类型,流类型可以分为STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING,STREAM_MUSIC 和 STREAM_ALARM四种类型。在AudioManager中定义。

③指定声音品质(采样率变换质量),一般直接设置为0!

在低版本中可以用上述构造方法,而API 21(Android 5.0)后这个构造方法就过时了! 而用到一个SoundPool.Builder的东东,我们要实例化SoundPool只需调用:

SoundPool.Builder spb = new SoundPool.Builder();

spb.setMaxStreams(10);

spb.setAudioAttributes(null); //转换音频格式

SoundPool sp = spb.build(); //创建SoundPool对象

要使用上述代码的话,TargetSDK版本要设置大于等于21哦!而且如果minSDK版本小于21 会出现下面的提醒:

6515e0eb5c65?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

2)常用方法介绍:

①加载声音资源:

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) 上述方法都会返回一个声音的ID,后面我们可以通过这个ID来播放指定的声音

参数介绍:

context:上下文

resId:资源id

priority:没什么用的一个参数,建议设置为1,保持和未来的兼容性

path:文件路径

FileDescriptor:貌似是流吧,这个我也不知道

AssetFileDescriptor:从asset目录读取某个资源文件,用法:

AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3");

②播放控制:

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。

③资源释放:

可以调用release()方法释放所有SoundPool对象占据的内存和资源,当然也可以根据声音 ID来释放!

3.使用代码示例:

运行效果图:

6515e0eb5c65?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

当点击按钮的时候会,"Duang"一下,这里演示了两种load的方法,分别是raw和assests!

关键代码:

MainActivity.java:

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;

}

代码非常简单,另外如果你点击了最后一个按钮的话,SoundPool就会被释放,然后再其他按钮 就不会Duang了哦~

4.OnLoadCompleteListener监听声音文件是否加载完毕

嗯,这个是临时想起的,写完在写另一篇的时候突然想起,用法也很简单,我们可以 往上面的代码中添加OnLoadCompleteListener这个东东,然后重写onLoadComplete()方法 ,最后为SoundPool对象设置这个东东即可!

mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {

@Override

public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

Toast.makeText(MainActivity.this,"加特技准备完毕~",Toast.LENGTH_SHORT).show();

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值