samples/Support4Demos/src/main/java/com/example/android/supportv4/media/Playback.java
@Override
    public void onAudioFocusChange(int focusChange) {
        Log.d(TAG, "onAudioFocusChange. focusChange=" + focusChange);
        if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // We have gained focus:
            mAudioFocus = AUDIO_FOCUSED;

        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS ||
                focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
            // We have lost focus. If we can duck (low playback volume), we can keep playing.
            // Otherwise, we need to pause the playback.
            boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
            mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

            // If we are playing, we need to reset media player by calling configMediaPlayerState
            // with mAudioFocus properly set.
            if (mState == PlaybackState.STATE_PLAYING && !canDuck) {
                // If we don't have audio focus and can't duck, we save the information that
                // we were playing, so that we can resume playback once we get the focus back.
                mPlayOnFocusGain = true;
            }
        } else {
            Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
        }
        configMediaPlayerState();
    }
/**
     * Called by AudioManager on audio focus changes.
     * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
     */
Called by AudioManager on audio focus changes.
Called by AudioManager on audio focus changes .
@Override
    public void onAudioFocusChange(int focusChange) {
        Log.d(TAG, "onAudioFocusChange. focusChange=" + focusChange);
        if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            mAudioFocus = AUDIO_FOCUSED;
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS ||
                focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
            boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
            mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;
            if (mState == PlaybackState.STATE_PLAYING && !canDuck) {
                mPlayOnFocusGain = true;
            }
        } else {
            Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
        }
        configMediaPlayerState();
    }
@ Override public void onAudioFocusChange ( int focusChange ) { Log . d ( TAG , "onAudioFocusChange. focusChange=" + focusChange ) ; if ( focusChange == AudioManager . AUDIOFOCUS_GAIN ) { mAudioFocus = AUDIO_FOCUSED ; } else if ( focusChange == AudioManager . AUDIOFOCUS_LOSS || focusChange == AudioManager . AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager . AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK ) { boolean canDuck = focusChange == AudioManager . AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK ; mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK ; if ( mState == PlaybackState . STATE_PLAYING && ! canDuck ) { mPlayOnFocusGain = true ; } } else { Log . e ( TAG , "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange ) ; } configMediaPlayerState ( ) ; }
Called by AudioManager on audio focus changes.
Called by AudioManager on audio focus changes .
// We have gained focus: // We have lost focus. If we can duck (low playback volume), we can keep playing. // Otherwise, we need to pause the playback. // If we are playing, we need to reset media player by calling configMediaPlayerState // with mAudioFocus properly set. // If we don't have audio focus and can't duck, we save the information that // we were playing, so that we can resume playback once we get the focus back.