最近做了一个语音助手的应用,刚开始考虑的问题不全面,后来自己测试音乐App播放时再次打开语音助手音乐没有暂停播放,导致无法进行语音识别。意识到是音频焦点的问题,就想到AudioManager的requestAudioFocus();
开始请求,发现被弃用了,接着查看API
看到了
[requestAudioFocus](https://developer.android.google.cn/reference/android/media/AudioManager.html#requestAudioFocus(android.media.AudioManager.OnAudioFocusChangeListener,%20int,%20int))([AudioManager.<wbr>OnAudioFocusChangeListener](https://developer.android.google.cn/reference/android/media/AudioManager.OnAudioFocusChangeListener.html) l,<wbr> int streamType,<wbr> int durationHint)
This method was deprecated in API level 26. use [requestAudioFocus(AudioFocusRequest)](https://developer.android.google.cn/reference/android/media/AudioManager.html#requestAudioFocus(android.media.AudioFocusRequest))
下面是我写的获取音频焦点的工具类
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
public class AudioHelper {
/**
* 用AudioManager获取音频焦点避免音视频声音并发问题
*/
private static AudioManager mAudioManager;
private static AudioManager.OnAudioFocusChangeListener mAudioFocusChangeListener;
private static AudioFocusRequest mAudioFocusRequest;
private static final String TAG = "AudioHelper";
public static void init(Context context) {
if (mAudioManager == null) {
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
if (mAudioFocusChangeListener == null) {
mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {//监听器
@Override
public void onAudioFocusChange(int focusChange) {
Log.e(TAG, "onAudioFocusChange=========" + focusChange);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
//继续播放
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
//暂停操作
break;
default:
break;
}
}
};
}
}
public static boolean isInit() {
return null != mAudioManager && null != mAudioFocusChangeListener;
}
public static int requestTheAudioFocus() {
if (Build.VERSION.SDK_INT < 8) {//Android 2.2开始(API8)才有音频焦点机制
return 0;
} else if (Build.VERSION.SDK_INT > 8 && Build.VERSION.SDK_INT <= 26) {
if (isInit()) {
int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
return result;
}
} else if (Build.VERSION.SDK_INT > 26) {//API26 废弃了原来的获取方法
if (isInit()) {
//下面两个常量参数试过很多 都无效,最终反编译了其他app才搞定,汗~
mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
.build())
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(mAudioFocusChangeListener)
.build();
int result = mAudioManager.requestAudioFocus(mAudioFocusRequest);
return result;
}
}
return 0;
}
public static void abandonFocus() {
if (isInit()) {
if (Build.VERSION.SDK_INT > 8 && Build.VERSION.SDK_INT <= 26) {
mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
} else {
if (Build.VERSION.SDK_INT > 26) {
mAudioManager.abandonAudioFocusRequest(mAudioFocusRequest);
}
}
}
}
}