List of usage examples for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT
int AUDIOFOCUS_LOSS_TRANSIENT
To view the source code for android.media AudioManager AUDIOFOCUS_LOSS_TRANSIENT.
Click Source Link
From source file:nuclei.media.playback.ExoPlayerPlayback.java
/** * Called by AudioManager on audio focus changes. * Implementation of {@link AudioManager.OnAudioFocusChangeListener} *///ww w . jav a2 s .c om @Override public void onAudioFocusChange(int focusChange) { if (LOG.isLoggable(Log.INFO)) LOG.d("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 == 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("onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange); } configMediaPlayerState(false, false); }
From source file:com.sourceauditor.sahomemonitor.MainActivity.java
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if (paused()) { play();/*from ww w .j ava 2s . co m*/ } else { audioPlayer.setVolume(1.0f, 1.0f); } break; case AudioManager.AUDIOFOCUS_LOSS: // Lost focus for an unbounded amount of time: pause playback pause(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: pause(); 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 (!paused()) { audioPlayer.setVolume(0.1f, 0.1f); } break; } }
From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java
@Override public void onAudioFocusChange(int focusState) { //Invoked when the audio focus of the system is updated. switch (focusState) { case AudioManager.AUDIOFOCUS_GAIN: // resume playback if (mediaPlayer == null) initMediaPlayer();/*from www. j a v a 2 s . c om*/ else if (!mediaPlayer.isPlaying()) mediaPlayer.start(); 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 if (mediaPlayer.isPlaying()) mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; 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.isPlaying()) mediaPlayer.pause(); 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.shinymayhem.radiopresets.ServiceRadioPlayer.java
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { mAudioFocused = false;/*from w w w .jav a 2s .com*/ 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.wojtechnology.sunami.TheBrain.java
private void registerAudio() { if (mHasAudioFocus || !mIsInit) { return;//from w w w . j av a2 s. c o m } mHasAudioFocus = true; // Add audio focus change listener mAFChangeListener = new AudioManager.OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { pausePlayback(); setUI(false); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Does nothing cause made me play music at work } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { pausePlayback(); unregisterAudio(); setUI(false); } } }; mAudioManager.requestAudioFocus(mAFChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); // Add headphone out listener registerReceiver(mNoisyAudioStreamReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); // Add notification and transport controls ComponentName eventReceiver = new ComponentName(getPackageName(), RemoteControlEventReceiver.class.getName()); mSession = new MediaSessionCompat(this, "FireSession", eventReceiver, null); mSession.setFlags( MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC); mSession.setMetadata(new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, "") .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, "") .putLong(MediaMetadata.METADATA_KEY_DURATION, -1).build()); mSession.setCallback(new MediaSessionCompat.Callback() { @Override public void onSeekTo(long pos) { super.onSeekTo(pos); setProgress((int) pos, isPlaying()); } }); mSession.setActive(true); }
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;//w w w. j ava 2 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;// w w w .j av a 2s . 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_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); }/*from w ww . java2 s . 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 ww w . j a va 2 s. com //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;//w ww .j a v a2s . 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); } }