好长时间没有写博客了,由于最近事情比较多。因此好长时间没有写博客了。坚持是一件很辛苦的事情。但还须要努力。。。好了,闲话不扯了。由于最近项目中用到了相应的短音频和震动的功能,因此这里总结一下相应的内容!java
在项目中忽然有个需求,就是实现短音频,比如短信音那种!最开始想到的方案就是MediaPlayer解决。可是我随手百度了一下,发现其实Android提供了一个单独播放短音频的 类(SoundPool),为何是它呢?由于soundPool用于播放密集,急促的短暂音效,例如微信的摇一摇等。。。SoundPool使用了音效池的来播放音频,若是超过流的最大数目,SoundPool会基于优先级自动中止先前播放的流。因此播放短促且密集的音频应该使用SoundPool进行相应的播放,好了就简单介绍到这里吧!android
这里说明一点,在android5.0的时候废弃了相应SoundPool使用构造方法的建立,因此为了兼容,在建立的时候要进行相应的适配!相应的适配代码以下:数组
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mSoundPool = new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.build();
} else {
mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, DEFAULT_QUALITY);
}
复制代码
其实就是几个重载的方法,基本上就是传入参数进行相应的加载资源。微信
上述方法都会返回一个声音的ID(这个ID对应后面的soundID),后面咱们能够经过这个ID来播放指定的声音。ide
由于使用的参数都差很少,因此这里统一进行讲解一下:函数
上面是开始播放的代码,参数含义以下:工具
以上就是SoundPool的一些常见API,基本上上面的代码就能够轻松的使用SoundPool了!oop
关于震动没有什么好说的,只能使用Vibrator进行开发,可是切记一点就是权限的声明,基本上只要相关的权限处理好了,震动仍是很容易的。哦,忘记了,这个也要进行相应的版本适配,下面会说到的。ui
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
复制代码
这个没有什么好说的,就是系统的相应服务。spa
几个相应的重载方法
其实调用这个就能够直接进行相应的震动了,因此说挺简单的。解释一下相应的参数含义
记得我上面说过关于版本适配的问题吧,其实在26版本也就是Android O的时候,须要进行相应的适配,使用了VibrationEffect封装了一层。
也就是说以前直接调用的代码经过VibrationEffect进行了相应的封装。解释一个参数
基本上关于短音频和振幅的内容就是上面这些,基本上按照上面这些进行相应的组合就能够了.可是为了你们的方便,我简单的封装了一个工具类给你们使用.封装的很差还请见谅!!!
public class SoundPoolUtils {
private static final int MAX_STREAMS = 2;
private static final int DEFAULT_QUALITY = 0;
private static final int DEFAULT_PRIORITY = 1;
private static final int LEFT_VOLUME = 1;
private static final int RIGHT_VOLUME = 1;
private static final int LOOP = 0;
private static final float RATE = 1.0f;
private static SoundPoolUtils sSoundPoolUtils;
/** * 音频的相关类 */
private SoundPool mSoundPool;
private Context mContext;
private Vibrator mVibrator;
private SoundPoolUtils(Context context) {
mContext = context;
//初始化行营的音频类
intSoundPool();
initVibrator();
}
/** * @author Angle * 建立时间: 2018/11/4 13:02 * 方法描述: 初始化短音频的内容 */
private void intSoundPool() {
//根据不一样的版本进行相应的建立
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mSoundPool = new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.build();
} else {
mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, DEFAULT_QUALITY);
}
}
/** * @author Angle * 建立时间: 2018/11/4 13:03 * 方法描述: 初始化震动的对象 */
private void initVibrator() {
mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
}
public static SoundPoolUtils getInstance(Context context) {
if (sSoundPoolUtils == null) {
synchronized (SoundPoolUtils.class) {
if (sSoundPoolUtils == null) {
sSoundPoolUtils = new SoundPoolUtils(context);
}
}
}
return sSoundPoolUtils;
}
/** * @param resId 音频的资源ID * @author Angle * 建立时间: 2018/11/4 13:03 * 方法描述: 开始播放音频 */
public void playVideo(int resId) {
if (mSoundPool == null) {
intSoundPool();
}
int load = mSoundPool.load(mContext, resId, DEFAULT_PRIORITY);
mSoundPool.play(load, LEFT_VOLUME, RIGHT_VOLUME, DEFAULT_PRIORITY, LOOP, RATE);
}
/** * @param milliseconds 震动时间 * @author Angle * 建立时间: 2018/11/4 13:04 * 方法描述: 开启相应的震动 */
public void startVibrator(long milliseconds) {
if (mVibrator == null) {
initVibrator();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
VibrationEffect vibrationEffect = VibrationEffect.createOneShot(milliseconds, 100);
mVibrator.vibrate(vibrationEffect);
} else {
mVibrator.vibrate(1000);
}
}
/** * @param resId 资源id * @param milliseconds 震动时间 * @author Angle * 建立时间: 2018/11/4 13:06 * 方法描述: 同时开始音乐和震动 */
public void startVideoAndVibrator(int resId, long milliseconds) {
playVideo(resId);
startVibrator(milliseconds);
}
/** * @author Angle * 建立时间: 2018/11/4 13:05 * 方法描述: 释放相应的资源 */
public void release() {
//释放全部的资源
if (mSoundPool != null) {
mSoundPool.release();
mSoundPool = null;
}
if (mVibrator != null) {
mVibrator.cancel();
mVibrator = null;
}
}
}
复制代码
由于咱们项目中,只用到了相应的播放一个短音频因此这里就只简单的封装了一下.见谅!!!
使用起来就很简单了,在你使用的地方直接调用
好了今天的内容就这么多了,有什么不明白的地方欢迎骚扰...