Java Code Examples for android.media.AudioManager#AUDIOFOCUS_LOSS
The following examples show how to use
android.media.AudioManager#AUDIOFOCUS_LOSS .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MusicControlAudioFocusListener.java From react-native-music-control with MIT License | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { abandonAudioFocus(); mPlayOnAudioFocus = false; emitter.onStop(); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { if (MusicControlModule.INSTANCE.isPlaying()) { mPlayOnAudioFocus = true; emitter.onPause(); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { volume.setCurrentVolume(40); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (volume.getCurrentVolume() != 100) { volume.setCurrentVolume(100); } if (mPlayOnAudioFocus) { emitter.onPlay(); } mPlayOnAudioFocus = false; } }
Example 2
Source File: FocusRequestChangeListenerImpl.java From Fatigue-Detection with MIT License | 6 votes |
public void onFocusChange(int state) { Log.d("FMediaPlayer", "Focus change current state is " + state); switch (state) { case AudioManager.AUDIOFOCUS_GAIN: Log.d("FMediaPlayer", "Focus change then start again"); fMediaPlayer.audioFocusGain(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Log.d("FMediaPlayer", "Focus change then pause isPlaying:" + fMediaPlayer.isPlaying()); fMediaPlayer.audioFocusLoss(fMediaPlayer.isPlaying()); break; case AudioManager.AUDIOFOCUS_LOSS: Log.d("FMediaPlayer", "Focus change then pause isPlaying:" + fMediaPlayer.isPlaying()); fMediaPlayer.audioFocusLoss(fMediaPlayer.isPlaying()); } }
Example 3
Source File: ReadAloudService.java From MyBookshelf with GNU General Public License v3.0 | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: // 重新获得焦点, 可做恢复播放,恢复后台音量的操作 if (!pause) { resumeReadAloud(); } break; case AudioManager.AUDIOFOCUS_LOSS: // 永久丢失焦点除非重新主动获取,这种情况是被其他播放器抢去了焦点, 为避免与其他播放器混音,可将音乐暂停 break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // 暂时丢失焦点,这种情况是被其他应用申请了短暂的焦点,可压低后台音量 if (!pause) { pauseReadAloud(false); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // 短暂丢失焦点,这种情况是被其他应用申请了短暂的焦点希望其他声音能压低音量(或者关闭声音)凸显这个声音(比如短信提示音), break; } }
Example 4
Source File: VideoUnit.java From Dashchan with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { if (!initialized) { return; } switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: { setPlaying(false, false); updatePlayState(); break; } case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: { boolean playing = player.isPlaying(); setPlaying(false, false); if (playing) { pausedByTransientLossOfFocus = true; } updatePlayState(); break; } case AudioManager.AUDIOFOCUS_GAIN: { if (pausedByTransientLossOfFocus) { setPlaying(true, false); } updatePlayState(); break; } } }
Example 5
Source File: PlayerAdapter.java From android-MediaBrowserService with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if (mPlayOnAudioFocus && !isPlaying()) { play(); } else if (isPlaying()) { setVolume(MEDIA_VOLUME_DEFAULT); } mPlayOnAudioFocus = false; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: setVolume(MEDIA_VOLUME_DUCK); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (isPlaying()) { mPlayOnAudioFocus = true; pause(); } break; case AudioManager.AUDIOFOCUS_LOSS: mAudioManager.abandonAudioFocus(this); mPlayOnAudioFocus = false; stop(); break; } }
Example 6
Source File: BackgroundAudioService.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { switch( focusChange ) { case AudioManager.AUDIOFOCUS_LOSS: { if( RemoteControlCallback.isPlaying() ) { RemoteControlCallback.stop(); initMediaSessionMetadata(); showPausedNotification(); } break; } case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: { RemoteControlCallback.pause(); initMediaSessionMetadata(); showPausedNotification(); break; } case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: { if( RemoteControlCallback.isPlaying() ) { RemoteControlCallback.setVolume(0.3f, 0.3f); } break; } case AudioManager.AUDIOFOCUS_GAIN: { if( !RemoteControlCallback.isPlaying() ) { RemoteControlCallback.play(); initMediaSessionMetadata(); showPlayingNotification(); } RemoteControlCallback.setVolume(1.0f, 1.0f); break; } } }
Example 7
Source File: AudioRecordingActivity.java From coursera-android with MIT License | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { mAudioManager.abandonAudioFocus(afChangeListener); // Stop playback, if necessary if (null != mPlayer && mPlayer.isPlaying()) stopPlaying(); } }
Example 8
Source File: RNJWPlayerView.java From react-native-jw-media-player with MIT License | 5 votes |
public void onAudioFocusChange(int focusChange) { if (mPlayer != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if (playbackDelayed || !userPaused) { synchronized(focusLock) { playbackDelayed = false; } boolean autostart = mPlayer.getConfig().getAutostart(); if (autostart) { mPlayer.play(); } } break; case AudioManager.AUDIOFOCUS_LOSS: synchronized(focusLock) { wasInterrupted = true; playbackDelayed = false; } mPlayer.pause(); hasAudioFocus = false; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: synchronized(focusLock) { wasInterrupted = true; playbackDelayed = false; } mPlayer.pause(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // ... pausing or ducking depends on your app break; } } else { lowerApiOnAudioFocus(focusChange); } } }
Example 9
Source File: MusicPlayer.java From Jockey with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { Timber.i("AudioFocus changed (%d)", focusChange); switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: Timber.i("Focus lost. Pausing music."); mFocused = false; mResumeOnFocusGain = false; pause(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Timber.i("Focus lost transiently. Pausing music."); boolean resume = isPlaying() || mResumeOnFocusGain; mFocused = false; pause(); mResumeOnFocusGain = resume; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Timber.i("Focus lost transiently. Letting system duck."); } else { Timber.i("Focus lost transiently. Ducking."); mMediaPlayer.setVolume(DUCK_VOLUME); } break; case AudioManager.AUDIOFOCUS_GAIN: Timber.i("Regained AudioFocus"); mMediaPlayer.setVolume(1f); if (mResumeOnFocusGain) play(); mResumeOnFocusGain = false; break; default: Timber.i("Ignoring AudioFocus state change"); break; } updateNowPlaying(); updateUi(); }
Example 10
Source File: VolumeHelper.java From Saiy-PS with GNU Affero General Public License v3.0 | 5 votes |
@Override public void onAudioFocusChange(final int focusChange) { if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: " + focusChange); } switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_GAIN"); } break; case AudioManager.AUDIOFOCUS_LOSS: if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS"); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT"); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); } break; default: if (DEBUG) { MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS default"); } break; } }
Example 11
Source File: FocusRequester.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Handle the loss of focus resulting from a given focus gain. * @param focusGain the focus gain from which the loss of focus is resulting * @param frWinner the new focus owner * @return true if the focus loss is definitive, false otherwise. */ @GuardedBy("MediaFocusControl.mAudioFocusLock") boolean handleFocusLossFromGain(int focusGain, final FocusRequester frWinner, boolean forceDuck) { final int focusLoss = focusLossForGainRequest(focusGain); handleFocusLoss(focusLoss, frWinner, forceDuck); return (focusLoss == AudioManager.AUDIOFOCUS_LOSS); }
Example 12
Source File: VideoPlayerView.java From TigerVideo with Apache License 2.0 | 5 votes |
/************************ 音频焦点获取与释放 ********************************/ @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if(PlayerManager.getInstance().getState() == PlayState.STATE_PAUSE) { PlayerManager.getInstance().play(); } break; case AudioManager.AUDIOFOCUS_LOSS: //长时间失去Focus PlayerManager.getInstance().stop(); Utils.log("AudioManager.AUDIOFOCUS_LOSS"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: //短时间失去Focus if (PlayerManager.getInstance().isPlaying()) { PlayerManager.getInstance().pause(); } Utils.log("AudioManager.AUDIOFOCUS_LOSS_TRANSIENT"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: break; } }
Example 13
Source File: Playback.java From react-native-streaming-audio-player with MIT License | 5 votes |
/** * Called by AudioManager on audio focus changes. * Implementation of {@link AudioManager.OnAudioFocusChangeListener} */ @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(); }
Example 14
Source File: AudioRecordingActivity.java From coursera-android with MIT License | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { mAudioManager.abandonAudioFocus(afChangeListener); // Stop playback, if necessary if (null != mPlayer && mPlayer.isPlaying()) stopPlaying(); } }
Example 15
Source File: PlaybackManager.java From android-music-player with Apache License 2.0 | 5 votes |
/** * Called by AudioManager on audio focus changes. * Implementation of {@link AudioManager.OnAudioFocusChangeListener} */ @Override public void onAudioFocusChange(int focusChange) { boolean gotFullFocus = false; boolean canDuck = false; if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { gotFullFocus = true; } 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. canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK; } if (gotFullFocus || canDuck) { if (mMediaPlayer != null) { if (mPlayOnFocusGain) { mPlayOnFocusGain = false; mMediaPlayer.start(); mState = PlaybackState.STATE_PLAYING; updatePlaybackState(); } float volume = canDuck ? 0.2f : 1.0f; mMediaPlayer.setVolume(volume, volume); } } else if (mState == PlaybackState.STATE_PLAYING) { mMediaPlayer.pause(); mState = PlaybackState.STATE_PAUSED; updatePlaybackState(); } }
Example 16
Source File: AudioFocusRequest.java From Fatigue-Detection with MIT License | 5 votes |
public void onAudioFocusChange(int focusChange) { if(focusChange == AudioManager.AUDIOFOCUS_LOSS) { Log.d(TAG, "audio focus loss"); mAudioManager.abandonAudioFocus(this); } audioFocusRequest.fireFocusChange(); }
Example 17
Source File: AudioFocusManager.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
@Override public void onAudioFocusChange(int focusChange) { // Convert the platform focus change to internal state. switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: audioFocusState = AUDIO_FOCUS_STATE_LOST_FOCUS; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: audioFocusState = AUDIO_FOCUS_STATE_LOSS_TRANSIENT; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (willPauseWhenDucked()) { audioFocusState = AUDIO_FOCUS_STATE_LOSS_TRANSIENT; } else { audioFocusState = AUDIO_FOCUS_STATE_LOSS_TRANSIENT_DUCK; } break; case AudioManager.AUDIOFOCUS_GAIN: audioFocusState = AUDIO_FOCUS_STATE_HAVE_FOCUS; break; default: Log.w(TAG, "Unknown focus change type: " + focusChange); // Early return. return; } // Handle the internal state (change). switch (audioFocusState) { case AUDIO_FOCUS_STATE_NO_FOCUS: // Focus was not requested; nothing to do. break; case AUDIO_FOCUS_STATE_LOST_FOCUS: playerControl.executePlayerCommand(PLAYER_COMMAND_DO_NOT_PLAY); abandonAudioFocus(/* forceAbandon= */ true); break; case AUDIO_FOCUS_STATE_LOSS_TRANSIENT: playerControl.executePlayerCommand(PLAYER_COMMAND_WAIT_FOR_CALLBACK); break; case AUDIO_FOCUS_STATE_LOSS_TRANSIENT_DUCK: // Volume will be adjusted by the code below. break; case AUDIO_FOCUS_STATE_HAVE_FOCUS: playerControl.executePlayerCommand(PLAYER_COMMAND_PLAY_WHEN_READY); break; default: throw new IllegalStateException("Unknown audio focus state: " + audioFocusState); } float volumeMultiplier = (audioFocusState == AUDIO_FOCUS_STATE_LOSS_TRANSIENT_DUCK) ? AudioFocusManager.VOLUME_MULTIPLIER_DUCK : AudioFocusManager.VOLUME_MULTIPLIER_DEFAULT; if (AudioFocusManager.this.volumeMultiplier != volumeMultiplier) { AudioFocusManager.this.volumeMultiplier = volumeMultiplier; playerControl.setVolumeMultiplier(volumeMultiplier); } }
Example 18
Source File: AudioTrackManagerDualStreamType.java From apollo-DuerOS with Apache License 2.0 | 4 votes |
public void onAudioFocusChange(int focusChange) { boolean isPause = mModeService.getMode(focusChange); LogUtil.d(TAG, "isPause = " + isPause); switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: mAM.unregisterMediaButtonEventReceiver(mMediaButtonReceiverComponent); setMediaAudioFocusStatus(false); informMusicPause(); LogUtil.d(TAG, "music AUDIOFOCUS_LOSS"); MsgHandlerCenter.dispatchMessage(CommonParams.MSG_CMD_AUDIO_FOCUS_LOSS); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: mAM.unregisterMediaButtonEventReceiver(mMediaButtonReceiverComponent); setMediaAudioFocusStatus(false); if (isPause) { informMusicPause(); } LogUtil.d(TAG, "music AUDIOFOCUS_LOSS_TRANSIENT"); MsgHandlerCenter.dispatchMessage(CommonParams.MSG_CMD_AUDIO_FOCUS_LOSS_TRANSIENT); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // setMediaAudioFocusStatus(false); LogUtil.d(TAG, "music AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); MsgHandlerCenter.dispatchMessage(CommonParams.MSG_CMD_AUDIO_FOCUS_LOSS_TRANSIENT_CAN_DUCK); break; case AudioManager.AUDIOFOCUS_GAIN: mAM.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent); setMediaAudioFocusStatus(true); // For HU from YF, it is possible to get audio focus in failure when resume playing // Try to resume playing when audio focus regain if (!isPause || getAudioFocusFailedWhenResume) { informMusicResume(); getAudioFocusFailedWhenResume = false; } LogUtil.d(TAG, "music AUDIOFOCUS_GAIN"); MsgHandlerCenter.dispatchMessage(CommonParams.MSG_CMD_AUDIO_FOCUS_GAIN); break; default: break; } }
Example 19
Source File: ServicePlayMusic.java From Bop with Apache License 2.0 | 4 votes |
/** * Does something when the audio focus state changed * * @note Meaning it runs when we get and when we don't get * the audio focus from `#requestAudioFocus()`. * <p> * For example, when we receive a message, we lose the focus * and when the ringer stops playing, we get the focus again. * <p> * So we must avoid the bug that occurs when the user pauses * the player but receives a message - and since after that * we get the focus, the player will unpause. */ public void onAudioFocusChange(int focusChange) { switch (focusChange) { // Yay, gained audio focus! Either from losing it for // a long or short periods of time. case AudioManager.AUDIOFOCUS_GAIN: if (player == null) initMusicPlayer(); if (pausedTemporarilyDueToAudioFocus) { pausedTemporarilyDueToAudioFocus = false; unpausePlayer(); } if (loweredVolumeDueToAudioFocus) { loweredVolumeDueToAudioFocus = false; player.setVolume(1.0f, 1.0f); } break; // Damn, lost the audio focus for a (presumable) long time case AudioManager.AUDIOFOCUS_LOSS: stopMusicPlayer(); break; // Just lost audio focus but will get it back shortly case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (!isPaused()) { pausePlayer(); pausedTemporarilyDueToAudioFocus = true; } break; // Temporarily lost audio focus but I can keep it playing // at a low volume instead of stopping completely case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: Log.w(TAG, "audiofocus loss transient can duck"); player.setVolume(0.1f, 0.1f); loweredVolumeDueToAudioFocus = true; break; } }
Example 20
Source File: MusicService.java From Muzesto with GNU General Public License v3.0 | 4 votes |
@Override public void handleMessage(final Message msg) { final MusicService service = mService.get(); if (service == null) { return; } synchronized (service) { switch (msg.what) { case FADEDOWN: mCurrentVolume -= .05f; if (mCurrentVolume > .2f) { sendEmptyMessageDelayed(FADEDOWN, 10); } else { mCurrentVolume = .2f; } service.mPlayer.setVolume(mCurrentVolume); break; case FADEUP: mCurrentVolume += .01f; if (mCurrentVolume < 1.0f) { sendEmptyMessageDelayed(FADEUP, 10); } else { mCurrentVolume = 1.0f; } service.mPlayer.setVolume(mCurrentVolume); break; case SERVER_DIED: if (service.isPlaying()) { final TrackErrorInfo info = (TrackErrorInfo) msg.obj; service.sendErrorMessage(info.mTrackName); service.removeTrack(info.mId); } else { service.openCurrentAndNext(); } break; case TRACK_WENT_TO_NEXT: service.setAndRecordPlayPos(service.mNextPlayPos); service.setNextTrack(); if (service.mCursor != null) { service.mCursor.close(); service.mCursor = null; } service.updateCursor(service.mPlaylist.get(service.mPlayPos).mId); service.notifyChange(META_CHANGED); service.updateNotification(); break; case TRACK_ENDED: if (service.mRepeatMode == REPEAT_CURRENT) { service.seek(0); service.play(); } else { service.gotoNext(false); } break; case RELEASE_WAKELOCK: service.mWakeLock.release(); break; case FOCUSCHANGE: if (D) Log.d(TAG, "Received audio focus change event " + msg.arg1); switch (msg.arg1) { case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (service.isPlaying()) { service.mPausedByTransientLossOfFocus = msg.arg1 == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT; } service.pause(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: removeMessages(FADEUP); sendEmptyMessage(FADEDOWN); break; case AudioManager.AUDIOFOCUS_GAIN: if (!service.isPlaying() && service.mPausedByTransientLossOfFocus) { service.mPausedByTransientLossOfFocus = false; mCurrentVolume = 0f; service.mPlayer.setVolume(mCurrentVolume); service.play(); } else { removeMessages(FADEDOWN); sendEmptyMessage(FADEUP); } break; default: } break; default: break; } } }