标签:
private AudioManager.OnAudioFocusChangeListener mAudioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN: // 之前的音量 1
if (mAudioManager != null)
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
(int) (mMaxVolum * 0.8), 0);
if (mIsLossAutioFocus) {
weDriveResume();
mIsLossAutioFocus = false;
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // 降音处理 -3
if (mAudioManager != null)
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
(int) (mMaxVolum * 0.3), 0);
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // 失去音频焦点 暂停播放 -2
mIsLossAutioFocus = true;
weDrivePause();
break;
case AudioManager.AUDIOFOCUS_LOSS: // -1
// weDrivePause();
break;
default:
break;
}
}
};
// 请求焦点
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager != null)
mAudioManager.requestAudioFocus(mAudioFocusListener,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
// 放弃焦点
if (mAudioManager != null)
mAudioManager.abandonAudioFocus(mAudioFocusListener);
标签:
原文地址:http://www.cnblogs.com/wutianlong/p/5730192.html