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: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);
                        }/*from w  ww.  ja  v a2  s .c  om*/
                    }
                } 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: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   www  .j  a v  a 2s  .co 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  w w  w.java 2s .  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 w  w .  java 2s. 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);/* ww  w  .jav  a 2s . co 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;
    }
}

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

@Override
public void onVisibleBehindCanceled() {
    stopTv("onVisibleBehindCanceled()", false);
    mTracker.sendScreenView("");
    mAudioFocusStatus = AudioManager.AUDIOFOCUS_LOSS;
    mAudioManager.abandonAudioFocus(this);
    if (mMediaSession.isActive()) {
        mMediaSession.setActive(false);// ww w  .  ja v a 2 s .c  o  m
    }
    stopPip();
    mVisibleBehind = false;
    if (!mOtherActivityLaunched && Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
        // Workaround: in M, onStop is not called, even though it should be called after
        // onVisibleBehindCanceled is called. As a workaround, we call finish().
        finish();
    }
    super.onVisibleBehindCanceled();
}