Example usage for android.media AudioManager AUDIOFOCUS_GAIN

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN

Introduction

In this page you can find the example usage for android.media AudioManager AUDIOFOCUS_GAIN.

Prototype

int AUDIOFOCUS_GAIN

To view the source code for android.media AudioManager AUDIOFOCUS_GAIN.

Click Source Link

Document

Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.

Usage

From source file:com.example.android.supportv4.media.Playback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 *///from   w  w w  . j a v a  2  s.co m
@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();
}

From source file:com.appdevper.mediaplayer.app.LocalPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*  www.  j  a  v  a2  s .  c  o m*/
@Override
public void onAudioFocusChange(int focusChange) {
    LogHelper.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 {
        LogHelper.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

From source file:info.tongrenlu.music.LocalPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 */// w  w  w .j  a v a  2s  .co m
@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 == PlaybackStateCompat.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();
}

From source file:nuclei.media.playback.FallbackPlayback.java

/**
 * Try to get the system audio focus.//from w w  w  .  j  a  v a  2s  .  c  om
 */
private void tryToGetAudioFocus() {
    if (mAudioFocus != AUDIO_FOCUSED) {
        int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mAudioFocus = AUDIO_FOCUSED;
        }
    }
}

From source file:org.videolan.vlc.MediaService.java

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;//from   w  ww . j a  v a 2 s.c  om

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {

            @Override
            public void onAudioFocusChange(int focusChange) {
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        stop();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        pause();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    LibVLC.getExistingInstance().setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    LibVLC.getExistingInstance().setVolume(100);
                    break;
                default:
                    break;
                }
            }
        };
    }

    Log.i(TAG, "changeAudioFocus gain=" + gain);
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:com.yamin.kk.service.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;//from w w w . ja va2 s.  c om

    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                /*
                 * Lower the volume to 36% to "duck" when an alert or something
                 * needs to be played.
                 */
                LibVLC.getExistingInstance().setVolume(36);
            } else {
                LibVLC.getExistingInstance().setVolume(100);
            }
        }
    };

    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:dk.nota.lyt.libvlc.PlaybackService.java

private OnAudioFocusChangeListener createOnAudioFocusChangeListener() {
    return new OnAudioFocusChangeListener() {
        private boolean mLossTransient = false;
        private boolean mLossTransientCanDuck = false;
        private boolean wasPlaying = false;

        @Override//from www .  j av a 2  s. c  o m
        public void onAudioFocusChange(int focusChange) {
            /*
             * Pause playback during alerts and notifications
             */
            switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS:
                Log.d(TAG, "AUDIOFOCUS_LOSS");
                // Pause playback
                changeAudioFocus(false);
                pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
                // Pause playback
                mLossTransient = true;
                wasPlaying = isPlaying();
                if (wasPlaying)
                    pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                // Lower the volume
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.setVolume(36);
                    mLossTransientCanDuck = true;
                }
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                Log.d(TAG, "AUDIOFOCUS_GAIN: " + mLossTransientCanDuck + ", " + mLossTransient);
                // Resume playback
                if (mLossTransientCanDuck) {
                    mMediaPlayer.setVolume(100);
                    mLossTransientCanDuck = false;
                } else if (mLossTransient) {
                    if (wasPlaying)
                        mMediaPlayer.play();
                    mLossTransient = false;
                }
                break;
            }
        }
    };
}

From source file:com.android.onemedia.playback.LocalRenderer.java

@Override
public void onAudioFocusChange(int focusChange) {
    // TODO figure out appropriate logic for handling focus loss at the TUQ
    // level./*  ww w. ja va  2 s .com*/
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mState == STATE_PLAYING) {
            onPause();
            mPlayOnReady = true;
        }
        mHasAudioFocus = false;
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
        if (mState == STATE_PLAYING) {
            onPause();
            mPlayOnReady = false;
        }
        pushOnFocusLost();
        mHasAudioFocus = false;
        break;
    case AudioManager.AUDIOFOCUS_GAIN:
        mHasAudioFocus = true;
        if (mPlayOnReady) {
            onPlay();
        }
        break;
    default:
        Log.d(TAG, "Unknown focus change event " + focusChange);
        break;
    }
}

From source file:org.videolan.myvlc.core.mediaController.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;/* w w w .java 2s . c  om*/

    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                /*
                 * Lower the volume to 36% to "duck" when an alert or something
                 * needs to be played.
                 */
                //LibVLC.getExistingInstance().setVolume(36);
            } else {
                //LibVLC.getExistingInstance().setVolume(100);
            }
        }
    };

    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:net.simno.klingar.playback.LocalPlayback.java

@Override
public void onAudioFocusChange(int focusChange) {
    Timber.d("onAudioFocusChange focusChange %s", focusChange);
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        audioFocus = AUDIO_FOCUSED; // We have gained focus
    } 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;
        audioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset ExoPlayer by calling configExoPlayerState
        // with audioFocus properly set.
        if (state == PlaybackStateCompat.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.
            playOnFocusGain = true;//www  . j av a2 s .c om
        }
    } else {
        Timber.e("onAudioFocusChange: Ignoring unsupported focusChange: %s", focusChange);
    }
    configExoPlayerState();
}