Example usage for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT

List of usage examples for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT

Introduction

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

Prototype

int AUDIOFOCUS_LOSS_TRANSIENT

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

Click Source Link

Document

Used to indicate a transient loss of audio focus.

Usage

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 a  2 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;
        }// w  w w.  j ava2s .c  om
    }
    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 www  .  jav  a 2s.  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;
    }
}