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.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java

@Override
public void onAudioFocusChange(int focusChange) {
    LogHelper.d(TAG, "onAudioFocusChange. focusChange = ", focusChange);

    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        mAudioFocus = AUDIO_FOCUSED;/*from   w w  w. j av  a  2 s.  c  o  m*/
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        if (mState == PlaybackState.STATE_PLAYING && !canDuck) {
            mPlayOnFocusGain = true;
        }
    } else {
        LogHelper.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

From source file:dk.dr.radio.afspilning.Afspiller.java

    /**
 * Responding to the loss of audio focus
 *//*from   ww w  . jav a 2s. c  o  m*/
@SuppressLint("NewApi")
private OnAudioFocusChangeListener getOnAudioFocusChangeListener() {
  if (onAudioFocusChangeListener == null)
    onAudioFocusChangeListener = new OnAudioFocusChangeListener() {

      //private int lydstyreFrDuck = -1;

      @TargetApi(Build.VERSION_CODES.FROYO)
      public void onAudioFocusChange(int focusChange) {
        Log.d("onAudioFocusChange " + focusChange);
        AudioManager am = (AudioManager) App.instans.getSystemService(Context.AUDIO_SERVICE);

        switch (focusChange) {
          // Kommer ved f.eks. en SMS eller taleinstruktion i Google Maps
          case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
            Log.d("JPER duck");
            if (afspillerstatus != Status.STOPPET) {
              // Vi 'dukker' lyden mens den vigtigere lyd hres
              // St lydstyrken ned til en 1/3-del
              //lydstyreFrDuck = am.getStreamVolume(AudioManager.STREAM_MUSIC);
              //am.setStreamVolume(AudioManager.STREAM_MUSIC, (lydstyreFrDuck + 2) / 3, 0);
              mediaPlayer.setVolume(0.1f, 0.1f); // logaritmisk skala - 0.1 svarer til 1/3-del
            }
            break;

          // Dette sker ved f.eks. opkald
          case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
            Log.d("JPER pause");
            if (afspillerstatus != Status.STOPPET) {
              pauseAfspilning(); // stter afspilningPPause=false
              if (afspillerlyde) afspillerlyd.stop.start();
              afspilningPPause = true;
            }
            break;

          // Dette sker hvis en anden app med lyd startes, f.eks. et spil
          case (AudioManager.AUDIOFOCUS_LOSS):
            Log.d("JPER stop");
            stopAfspilning();
            am.abandonAudioFocus(this);
            break;

          // Dette sker nr opkaldet er slut og ved f.eks. opkald
          case (AudioManager.AUDIOFOCUS_GAIN):
            Log.d("JPER Gain");
            if (afspillerstatus == Status.STOPPET) {
              if (afspilningPPause) startAfspilningIntern();
            } else {
              // Genskab lydstyrke fr den blev dukket
              mediaPlayer.setVolume(1f, 1f);
              //if (lydstyreFrDuck > 0) {
              //  am.setStreamVolume(AudioManager.STREAM_MUSIC, lydstyreFrDuck, 0);
              //}
              // Genstart ikke afspilning, der spilles allerede!
              //startAfspilningIntern();
            }
        }
      }
    };
  return (OnAudioFocusChangeListener) onAudioFocusChangeListener;
}

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

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;/*from  ww w  . j a  v  a  2  s  .c  om*/

    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                /*
                 * Lower the volume to 36% to "duck" when an alert or something
                 * needs to be played.
                 */
                LibVLC.getExistingInstance().setVolume(36);
            } else {
                LibVLC.getExistingInstance().setVolume(100);
            }
        }
    };

    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.amahi.anywhere.service.AudioService.java

@Override
public void onAudioFocusChange(int audioFocus) {
    switch (audioFocus) {
    case AudioManager.AUDIOFOCUS_GAIN:
        handleAudioFocusGain();//from  ww  w . ja  v a2  s.c o  m
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        handleAudioFocusLoss();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        handleAudioFocusDuck();
        break;

    default:
        break;
    }
}

From source file:com.allthatseries.RNAudioPlayer.Playback.java

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

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

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;//from   w  w  w.j av a 2  s  .com

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {

            @Override
            public void onAudioFocusChange(int focusChange) {
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        stop();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (LibVLC.getExistingInstance().isPlaying()) {
                        pause();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    LibVLC.getExistingInstance().setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    LibVLC.getExistingInstance().setVolume(100);
                    break;
                default:
                    break;
                }
            }
        };
    }

    Log.i(TAG, "changeAudioFocus gain=" + gain);
    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 OnAudioFocusChangeListener createOnAudioFocusChangeListener() {
    return new OnAudioFocusChangeListener() {
        private boolean mLossTransient = false;
        private boolean mLossTransientCanDuck = false;
        private boolean wasPlaying = false;

        @Override/*from ww  w  .j  av  a  2s. com*/
        public void onAudioFocusChange(int focusChange) {
            /*
             * Pause playback during alerts and notifications
             */
            switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS:
                Log.d(TAG, "AUDIOFOCUS_LOSS");
                // Pause playback
                changeAudioFocus(false);
                pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
                // Pause playback
                mLossTransient = true;
                wasPlaying = isPlaying();
                if (wasPlaying)
                    pause();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                // Lower the volume
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.setVolume(36);
                    mLossTransientCanDuck = true;
                }
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                Log.d(TAG, "AUDIOFOCUS_GAIN: " + mLossTransientCanDuck + ", " + mLossTransient);
                // Resume playback
                if (mLossTransientCanDuck) {
                    mMediaPlayer.setVolume(100);
                    mLossTransientCanDuck = false;
                } else if (mLossTransient) {
                    if (wasPlaying)
                        mMediaPlayer.play();
                    mLossTransient = false;
                }
                break;
            }
        }
    };
}

From source file:com.customprogrammingsolutions.MediaStreamer.MediaStreamerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    //very inefficient...try optimizing when you have time  
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        stop();//from   w w  w.  jav a 2  s  .c  o m
        play();
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // Lost focus for an unbounded amount of time: stop playback and release media player
        stop();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // Lost focus for a short time, but we have to stop
        stop();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        stop();
        break;
    }

}

From source file:com.example.android.supportv4.media.Playback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 *///from  ww  w.j  ava 2  s.  co  m
@Override
public void onAudioFocusChange(int focusChange) {
    Log.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 == PlaybackState.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(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState();
}

From source file:com.yamin.kk.service.AudioService.java

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

    audioFocusListener = new OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                /*
                 * Lower the volume to 36% to "duck" when an alert or something
                 * needs to be played.
                 */
                LibVLC.getExistingInstance().setVolume(36);
            } else {
                LibVLC.getExistingInstance().setVolume(100);
            }
        }
    };

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

}