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:org.y20k.transistor.PlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    // gain of audio focus of unknown duration
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mPlayback) {
            if (mMediaPlayer == null) {
                initializeMediaPlayer();
            } else if (!mMediaPlayer.isPlaying()) {
                mMediaPlayer.start();//w  ww  .ja v a2 s  . c o  m
            }
            mMediaPlayer.setVolume(1.0f, 1.0f);
        }
        break;
    // loss of audio focus of unknown duration
    case AudioManager.AUDIOFOCUS_LOSS:
        stopPlayback();
        break;
    // transient loss of audio focus
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        if (!mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            stopPlayback();
        } else if (mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
        }
        break;
    // temporary external request of audio focus
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.setVolume(0.1f, 0.1f);
        }
        break;
    }
}

From source file:bala.padio.Player.java

@Override
public void onAudioFocusChange(int focusChange) {
    Log.e(TAG, "onAudioFocusChange: " + focusChange);
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        // resume playback
        if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
            Play(playerUrl);//from  w w  w. j a  va  2  s  . c om
        }
        mediaPlayer.setVolume(1.0f, 1.0f);
        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
        // playback. We don't release the media player because playback
        // is likely to resume
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.reset();
        }
        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
        if (mediaPlayer.isPlaying())
            mediaPlayer.setVolume(0.1f, 0.1f);
        break;
    }
}

From source file:com.ironsmile.cordova.mediaevents.MediaEventListener.java

/**
 * Updates the JavaScript side with new audio focus event
 *
 * @param focusEvent the received event/*from   w  ww .  j av  a 2  s .  c  o  m*/
 * @return
 */
private void sendFocusEvent(int focusEvent) {
    JSONObject obj = new JSONObject();
    try {
        switch (focusEvent) {
        case AudioManager.AUDIOFOCUS_GAIN:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            obj.put("type", "audiofocusgain");
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            obj.put("type", "audiofocusloss");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            obj.put("type", "audiofocuslosstransient");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            obj.put("type", "audiofocuslosstransientcanduck");
            break;
        }

    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    sendUpdate(obj, true);
}

From source file:com.example.android.miwok.activity.ColorsActivity.java

private AudioManager.OnAudioFocusChangeListener getAudioFocusChangeCallback() {

    return new AudioManager.OnAudioFocusChangeListener() {
        Handler mHandler = new Handler();

        public void onAudioFocusChange(int focusChange) {

            // The AUDIOFOCUS_LOSS case means we've lost audio focus permanently
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                // Stop playback and clean up resources
                releaseMediaPlayer();//  w w  w  .  ja v a2 s.co  m
            }
            // The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
            // short amount of time.
            else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                // Pause playback immediately
                mMediaPlayer.pause();
                // play the word from the beginning when we resume playback.
                mMediaPlayer.seekTo(0);
            }
            //The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
            // our app is allowed to continue playing sound but at a lower volume.
            else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                // Lower the volume, keep playing
                mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
            }
            // The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
            else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Raise volume to normal, restart playback if necessary
                mMediaPlayer.start();
                mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);

            }
        }
    };
}

From source file:com.reallynourl.nourl.fmpfoldermusicplayer.backend.MediaService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mMediaPlayer != null) {
            mMediaPlayer.setVolume(1.0f, 1.0f);
        }/*w  w w . j  av a 2  s  . c o  m*/
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
        abandonAudioFocus();
        if (mMediaPlayer != null) {
            releasePlayer();
        }
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.setVolume(0.15f, 0.15f);
        }
        break;
    }
}

From source file:com.mylovemhz.simplay.MusicService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        stop();// w  w w .j  av a2  s  . c o  m
        break;
    case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
        stopSelf();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        lowerVolume();
        break;
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
    case AudioManager.AUDIOFOCUS_GAIN:
        restoreVolume();
        break;
    }
}

From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mRingtone.isPlaying())
            mRingtone.stop();/*from   w  ww  .j av a2s  .c o m*/
        break;
    }
}

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

    /**
 * Responding to the loss of audio focus
 */// w  ww  .ja va  2 s  . co  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: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;/* w  ww  . jav  a 2s . co 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:org.videolan.vlc.AudioService.java

@TargetApi(8)
private void changeAudioFocus(boolean gain) {
    if (!Util.isFroyoOrLater()) // NOP if not supported
        return;//from  w  w  w. j a va2s . 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);

}