Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 用AudioManager获取音频焦点避免音视频声音重叠问题
*/
public class AudioFocusManager {
private static final String TAG = "AudioFocusManager";
private Context mContext;
private AudioManager mAudioManager;
private AudioManager.OnAudioFocusChangeListener mAudioFocusChangeListener;
private AudioFocusRequest mAudioFocusRequest;
public AudioFocusManager(Context context) {
mContext = context;
}
/**
* 请求音频焦点 设置监听
*/
public int requestAudioFocus(final AudioListener audioListener) {
if (mAudioManager == null) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
if (mAudioFocusChangeListener == null) {
mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {//监听器
@Override
public void onAudioFocusChange(int focusChange) {
Log.d(TAG, "onAudioFocusChange: " + focusChange);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_GAIN");
audioListener.play();
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_LOSS");
audioListener.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_LOSS_TRANSIENT");
audioListener.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// ... pausing or ducking depends on your app
Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
audioListener.pause();
break;
}
}
};
}
int requestFocusResult = 0;
if (mAudioManager != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//下面两个常量参数试过很多都无效,最终反编译了其他app才搞定,汗~
requestFocusResult = mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
Log.d(TAG, "requestAudioFocus: SDK_INT < 26 =" + requestFocusResult);
} else {
if (mAudioFocusRequest == null) {
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();
}
requestFocusResult = mAudioManager.requestAudioFocus(mAudioFocusRequest);
Log.d(TAG, "requestAudioFocus: SDK_INT >= 26 =" + requestFocusResult);
}
}
return requestFocusResult;
}
/**
* 暂停、播放完成或退到后台释放音频焦点
* 应该先请求音频焦点
*/
public int releaseAudioFocus() {
if (mAudioManager == null) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
int abandonFocusResult = 0;
if (mAudioManager != null && mAudioFocusChangeListener != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
abandonFocusResult = mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
Log.d(TAG, "releaseAudioFocus: SDK_INT < 26 =" + abandonFocusResult);
} else {
if (mAudioFocusRequest != null) {
abandonFocusResult = mAudioManager.abandonAudioFocusRequest(mAudioFocusRequest);
Log.d(TAG, "releaseAudioFocus: SDK_INT >= 26 =" + abandonFocusResult);
}
}
}
return abandonFocusResult;
}
public interface AudioListener {
void play();
void pause();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.