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: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  v a2  s  .  c  o m

    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:dk.nota.lyt.libvlc.PlaybackService.java

private void changeAudioFocus(boolean acquire) {
    final AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (am == null)
        return;// w w  w  .  jav a 2 s  . co  m

    if (acquire) {
        if (!mHasAudioFocus) {
            final int result = am.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN);
            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                am.setParameters("bgm_state=true");
                mHasAudioFocus = true;
            }
        }
    } else {
        if (mHasAudioFocus) {
            final int result = am.abandonAudioFocus(mAudioFocusListener);
            am.setParameters("bgm_state=false");
            mHasAudioFocus = false;
        }
    }
}

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

/**
 * Try to get the system audio focus./*from  w  w  w. ja va2s.c o m*/
 */
private void tryToGetAudioFocus() {
    LOG.d("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:co.codecrunch.musicplayerlite.manager.MusicPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (ignoreAudioFocus) {
        ignoreAudioFocus = false;//from  www. j  a  v a2 s .c  om
        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:com.goftagram.telegram.messenger.MusicPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (ignoreAudioFocus) {
        ignoreAudioFocus = false;//from w  ww .  j a  va 2s  .  com
        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.j  a  va2  s .  co  m*/

    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:com.example.android.mediarouter.player.MainActivity.java

private void registerRemoteControlClient() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Create the RCC and register with AudioManager and MediaRouter
        mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        mAudioManager.registerMediaButtonEventReceiver(mEventReceiver);
        mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent);
        mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        mMediaRouter.addRemoteControlClient(mRemoteControlClient);
        SampleMediaButtonReceiver.setActivity(MainActivity.this);
        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
        mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }/*from   ww w  . ja  v  a2  s.  c  o m*/
}

From source file:com.example.android.mediarouter.player.RadioActivity.java

private void registerRemoteControlClient() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Create the RCC and register with AudioManager and MediaRouter
        mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        mAudioManager.registerMediaButtonEventReceiver(mEventReceiver);
        mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent);
        mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        mMediaRouter.addRemoteControlClient(mRemoteControlClient);
        SampleRadioButtonReceiver.setActivity(RadioActivity.this);
        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
        mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }/*w ww  . ja  v a 2  s . c om*/
}

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   ww  w .ja  v  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:org.videolan.vlc.PlaybackService.java

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

    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);

}