Example usage for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK

List of usage examples for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK

Introduction

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

Prototype

int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK

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

Click Source Link

Document

Used to indicate a transient loss of audio focus where the loser of the audio focus can lower its output volume if it wants to continue playing (also referred to as "ducking"), as the new focus owner doesn't require others to be silent.

Usage

From source file:com.owncloud.android.media.MediaService.java

/**
 * Called by the system when another app tries to play some sound.
 * /*from w  w  w. jav  a  2s.c o m*/
 * {@inheritDoc}
 */
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange > 0) {
        // focus gain; check AudioManager.AUDIOFOCUS_* values
        mAudioFocus = AudioFocus.FOCUS;
        // restart media player with new focus settings
        if (mState == State.PLAYING)
            configAndStartMediaPlayer();

    } else if (focusChange < 0) {
        // focus loss; check AudioManager.AUDIOFOCUS_* values
        boolean canDuck = AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK == focusChange;
        mAudioFocus = canDuck ? AudioFocus.NO_FOCUS_CAN_DUCK : AudioFocus.NO_FOCUS;
        // start/restart/pause media player with new focus settings
        if (mPlayer != null && mPlayer.isPlaying())
            configAndStartMediaPlayer();
    }

}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
        mAudioFocused = false;/*from  w  w w.  j  av a 2  s.c o m*/
        if (LOCAL_LOGD)
            log("AUDIOFOCUS_LOSS_TRANSIENT", "d");
        // Pause playback
        pause();
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        mAudioFocused = true;
        if (LOCAL_LOGD)
            log("AUDIOFOCUS_GAIN", "d");
        // Resume playback or unduck
        if (shouldResumeOnAudioFocus()) {
            if (mCurrentPlayerState.equals(STATE_PAUSED)) {
                resume();
            } else {
                //find out which states this is run into from. change shouldResumeOnAudioFocus() accordingly (only resume when paused?)
                if (LOCAL_LOGD)
                    log("Focus gained,  but playback not resumed", "d"); //investigate
            }
        } else {
            if (LOCAL_LOGD)
                log("Focused gained but playback should not try to resume", "d");
            if (mPreset == 0) {

            }

        }
        unduck();

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {

        if (LOCAL_LOGD)
            log("AUDIOFOCUS_LOSS", "d");
        if (mMediaButtonEventReceiverRegistered) {
            if (LOCAL_LOGV)
                log("unregister button receiver", "v");
            mAudioManager.unregisterMediaButtonEventReceiver(
                    new ComponentName(getPackageName(), ReceiverRemoteControl.class.getName()));
            mMediaButtonEventReceiverRegistered = false;
        } else {
            if (LOCAL_LOGV)
                log("button receiver already unregistered", "v");
        }
        //this.abandonAudioFocus(); //already done in stop()->pause()
        // Stop playback
        stop();

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        // Lower the volume
        if (LOCAL_LOGV)
            log("AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK", "v");
        duck();
    }

}

From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java

public static void requestAudioFocus(final Context context) {
    if (!hasFocus) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        hasFocus = true;//from  ww  w.  j  a  va2 s.c  om
        audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                DownloadServiceImpl downloadService = (DownloadServiceImpl) context;
                if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                        || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        && !downloadService.isJukeboxEnabled()) {
                    if (downloadService.getPlayerState() == PlayerState.STARTED) {
                        SharedPreferences prefs = getPreferences(context);
                        int lossPref = Integer
                                .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
                        if (lossPref == 2 || (lossPref == 1
                                && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
                            lowerFocus = true;
                            downloadService.setVolume(0.1f);
                        } else if (lossPref == 0
                                || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
                            pauseFocus = true;
                            downloadService.pause();
                        }
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    if (pauseFocus) {
                        pauseFocus = false;
                        downloadService.start();
                    } else if (lowerFocus) {
                        lowerFocus = false;
                        downloadService.setVolume(1.0f);
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) {
                    hasFocus = false;
                    downloadService.pause();
                    audioManager.abandonAudioFocus(this);
                }
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}

From source file:github.daneren2005.dsub.util.Util.java

@TargetApi(8)
public static void requestAudioFocus(final Context context) {
    if (Build.VERSION.SDK_INT >= 8 && !hasFocus) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        hasFocus = true;//from   w  w  w  .  j  a v a2 s  .c  om
        audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                DownloadServiceImpl downloadService = (DownloadServiceImpl) context;
                if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                        || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        && !downloadService.isJukeboxEnabled()) {
                    if (downloadService.getPlayerState() == PlayerState.STARTED) {
                        SharedPreferences prefs = getPreferences(context);
                        int lossPref = Integer
                                .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
                        if (lossPref == 2 || (lossPref == 1
                                && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
                            lowerFocus = true;
                            downloadService.setVolume(0.1f);
                        } else if (lossPref == 0
                                || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
                            pauseFocus = true;
                            downloadService.pause();
                        }
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    if (pauseFocus) {
                        pauseFocus = false;
                        downloadService.start();
                    } else if (lowerFocus) {
                        lowerFocus = false;
                        downloadService.setVolume(1.0f);
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) {
                    hasFocus = false;
                    downloadService.pause();
                    audioManager.abandonAudioFocus(this);
                }
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}

From source file:github.daneren2005.dsub.util.Util.java

@TargetApi(8)
public static void requestAudioFocus(final Context context) {
    if (Build.VERSION.SDK_INT >= 8 && focusListener == null) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                DownloadService downloadService = (DownloadService) context;
                if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                        || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        && !downloadService.isRemoteEnabled()) {
                    if (downloadService.getPlayerState() == PlayerState.STARTED) {
                        Log.i(TAG, "Temporary loss of focus");
                        SharedPreferences prefs = getPreferences(context);
                        int lossPref = Integer
                                .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
                        if (lossPref == 2 || (lossPref == 1
                                && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
                            lowerFocus = true;
                            downloadService.setVolume(0.1f);
                        } else if (lossPref == 0
                                || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
                            pauseFocus = true;
                            downloadService.pause(true);
                        }/*  w  ww.ja  v  a2s .c  o m*/
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    if (pauseFocus) {
                        pauseFocus = false;
                        downloadService.start();
                    } else if (lowerFocus) {
                        lowerFocus = false;
                        downloadService.setVolume(1.0f);
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) {
                    Log.i(TAG, "Permanently lost focus");
                    focusListener = null;
                    downloadService.pause();
                    audioManager.abandonAudioFocus(this);
                }
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}

From source file:com.rks.musicx.services.MusicXService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        Log.d(TAG, "AudioFocus Loss");
        if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) {
            pause();/*from   w w  w . j  a v  a2  s  . co  m*/
            //service.stopSelf();
        }
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) {
            MediaPlayerSingleton.getInstance().getMediaPlayer().setVolume(0.3f, 0.3f);
            mIsDucked = true;
        }
        Log.d(TAG, "AudioFocus Loss Can Duck Transient");
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        Log.d(TAG, "AudioFocus Loss Transient");
        if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) {
            pause();
            mLostAudioFocus = true;
        }
        break;
    case AudioManager.AUDIOFOCUS_GAIN:
        Log.d(TAG, "AudioFocus Gain");
        if (mIsDucked) {
            MediaPlayerSingleton.getInstance().getMediaPlayer().setVolume(1.0f, 1.0f);
            mIsDucked = false;
        } else if (mLostAudioFocus) {
            // If we temporarily lost the audio focus we can resume playback here
            if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) {
                play();
            }
            mLostAudioFocus = false;
        }
        break;
    default:
        Log.d(TAG, "Unknown focus");
    }
}

From source file:github.madmarty.madsonic.util.Util.java

@TargetApi(8)
public static void requestAudioFocus(final Context context) {
    if (Build.VERSION.SDK_INT >= 8 && !hasFocus) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        hasFocus = true;/*from ww w .  j  a  va2 s.c o  m*/
        audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                DownloadServiceImpl downloadService = (DownloadServiceImpl) context;
                if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                        || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        && !downloadService.isJukeboxEnabled()) {
                    if (downloadService.getPlayerState() == PlayerState.STARTED) {
                        SharedPreferences prefs = getPreferences(context);
                        int lossPref = Integer
                                .parseInt(prefs.getString(Constants.PREFERENCES_KEY_AUDIO_FOCUS, "1"));
                        if (lossPref == 2 || (lossPref == 1
                                && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
                            lowerFocus = true;
                            downloadService.setVolume(0.1f);
                        } else if (lossPref == 0
                                || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
                            pauseFocus = true;
                            downloadService.pause();
                        }
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    if (pauseFocus) {
                        pauseFocus = false;
                        downloadService.start();
                    } else if (lowerFocus) {
                        lowerFocus = false;
                        downloadService.setVolume(1.0f);
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) {
                    hasFocus = false;
                    downloadService.pause();
                    audioManager.abandonAudioFocus(this);
                }
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}

From source file:com.fastbootmobile.encore.service.PlaybackService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        // You have gained the audio focus.
        mNativeHub.setDucking(false);//from   ww  w.  jav  a2 s  . c o m
        if (mState != STATE_PLAYING && mPausedByFocusLoss) {
            playImpl();
            mPausedByFocusLoss = false;
        }
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // You have lost the audio focus for a presumably long time. You must stop all audio
        // playback.
        pauseImpl();
        mPausedByFocusLoss = true;
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // You have temporarily lost audio focus, but should receive it back shortly. You
        // must stop all audio playback, but you can keep your resources because you will
        // probably get focus back shortly.
        pauseImpl();
        mPausedByFocusLoss = true;
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // You have temporarily lost audio focus, but you are allowed to continue to play
        // audio quietly (at a low volume) instead of killing audio completely.
        mNativeHub.setDucking(true);
        break;
    }
}

From source file:com.android.tv.MainActivity.java

private void setVolumeByAudioFocusStatus(TunableTvView tvView) {
    SoftPreconditions.checkState(tvView == mTvView || tvView == mPipView);
    if (tvView.isPlaying()) {
        switch (mAudioFocusStatus) {
        case AudioManager.AUDIOFOCUS_GAIN:
            tvView.setStreamVolume(AUDIO_MAX_VOLUME);
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            tvView.setStreamVolume(AUDIO_MIN_VOLUME);
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            tvView.setStreamVolume(AUDIO_DUCKING_VOLUME);
            break;
        }/*from  w  ww .j  a  v  a2  s  . co  m*/
    }
    if (tvView == mTvView) {
        if (mPipView != null && mPipView.isPlaying()) {
            mPipView.setStreamVolume(AUDIO_MIN_VOLUME);
        }
    } else { // tvView == mPipView
        if (mTvView != null && mTvView.isPlaying()) {
            mTvView.setStreamVolume(AUDIO_MIN_VOLUME);
        }
    }
}

From source file:mp.teardrop.PlaybackService.java

public void onAudioFocusChange(int type) {
    Log.d("OrchidMP", "audio focus change: " + type);
    switch (type) {
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        mDuckedLoss = (mState & FLAG_PLAYING) != 0;
        unsetFlag(FLAG_PLAYING);/*from  w w w  . ja  va2 s.  c  o  m*/
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        mDuckedLoss = false;
        mForceNotificationVisible = true;
        unsetFlag(FLAG_PLAYING);
        break;
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mDuckedLoss) {
            mDuckedLoss = false;
            setFlag(FLAG_PLAYING);
        }
        break;
    }
}