Android点赞音效播放

时间:2023-03-08 19:50:37
Android点赞音效播放
    /**
* 音效播放
*/
private SoundPool mPool;
/**
* 音效id
*/
private int voiceID;
voiceID = initSoundPool();
/**
* 初始化SoundPool
*/
private int initSoundPool() {
/**
* 21版本后,SoundPool的创建发生很大改变
*/
//判断系统sdk版本,如果版本超过21,调用第一种
if (Build.VERSION.SDK_INT >= 21) {
SoundPool.Builder builder = new SoundPool.Builder();
builder.setMaxStreams(2);//传入音频数量
//AudioAttributes是一个封装音频各种属性的方法
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);//设置音频流的合适的属性
builder.setAudioAttributes(attrBuilder.build());//加载一个AudioAttributes
mPool = builder.build();
} else {
mPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
}
//load的返回值是一个int类的值:音频的id,在SoundPool的play()方法中加入这个id就能播放这个音频
return mPool.load(mContext, R.raw.dianzan, 1);
}
    // 播放点赞音效
mPool.play(voiceID, 1, 1, 0, 0, 1);

音效地址:http://pan.baidu.com/s/1pLbJr4n