Example usage for android.media AudioManager AUDIOFOCUS_LOSS

List of usage examples for android.media AudioManager AUDIOFOCUS_LOSS

Introduction

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

Prototype

int AUDIOFOCUS_LOSS

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

Click Source Link

Document

Used to indicate a loss of audio focus of unknown duration.

Usage

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./*from   www  .  ja  v a 2  s. co  m*/
    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.vlc.audio.AudioService.java

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

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or something
                     * needs to be played.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

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

}

From source file:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (ignoreAudioFocus) {
        ignoreAudioFocus = false;//from  w  ww .  j a  v  a 2s  . c o m
        return;
    }
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
        if (MediaController.getInstance().isPlayingAudio(MediaController.getInstance().getPlayingSongDetail())
                && !MediaController.getInstance().isAudioPaused()) {
            MediaController.getInstance().pauseAudio(MediaController.getInstance().getPlayingSongDetail());
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // MediaController.getInstance().playAudio(MediaController.getInstance().getPlayingMessageObject());
    }
}

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 .ja v a 2  s .  c  o  m
        }
    } else {
        Timber.e("onAudioFocusChange: Ignoring unsupported focusChange: %s", focusChange);
    }
    configExoPlayerState();
}

From source file:com.goftagram.telegram.messenger.MusicPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (ignoreAudioFocus) {
        ignoreAudioFocus = false;/*  w  w  w. j ava 2 s . c  o  m*/
        return;
    }
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
        if (MediaController.getInstance()
                .isPlayingAudio(MediaController.getInstance().getPlayingMessageObject())
                && !MediaController.getInstance().isAudioPaused()) {
            MediaController.getInstance().pauseAudio(MediaController.getInstance().getPlayingMessageObject());
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        //MediaController.getInstance().playAudio(MediaController.getInstance().getPlayingMessageObject());
    }
}

From source file:com.dzt.musicplay.player.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!LibVlcUtil.isFroyoOrLater()) // NOP if not supported
        return;/*ww w.  ja va2 s  .com*/

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or
                     * something needs to be played.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

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

}

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

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!AndroidUtil.isFroyoOrLater()) // NOP if not supported
        return;/*  w ww .  j  a  va 2  s . co m*/

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                if (!hasCurrentMedia())
                    return;
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (MediaPlayer().isPlaying())
                        MediaPlayer().pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or something
                     * needs to be played.
                     */
                    MediaPlayer().setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    MediaPlayer().setVolume(100);
                    break;
                }
            }
        };
    }

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

}

From source file:com.bayapps.android.robophish.playback.LocalPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 *///from  www  .jav a 2  s . c  om
@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 == 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 {
        LogHelper.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

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

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*  w ww  .  j  ava  2s.  com*/
@Override
public void onAudioFocusChange(int 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;
        }
    }
    configMediaPlayerState();
}

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

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *///  w  w w  . j  av a2  s  .  c  om
@Override
public void onAudioFocusChange(int focusChange) {
    if (LOG.isLoggable(Log.INFO))
        LOG.d("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("onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState(false, false);
}