Java Code Examples for android.media.AudioManager#AUDIOFOCUS_LOSS_TRANSIENT
The following examples show how to use
android.media.AudioManager#AUDIOFOCUS_LOSS_TRANSIENT .
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: MultiSampleVideo.java From GSYVideoPlayer with Apache License 2.0 | 6 votes |
@Override protected void init(Context context) { super.init(context); mCoverImage = (ImageView) findViewById(R.id.thumbImage); if (mThumbImageViewLayout != null && (mCurrentState == -1 || mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR)) { mThumbImageViewLayout.setVisibility(VISIBLE); } onAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: break; case AudioManager.AUDIOFOCUS_LOSS: //todo 判断如果不是外界造成的就不处理 break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: //todo 判断如果不是外界造成的就不处理 break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: break; } } }; }
Example 2
Source File: AudioTrackManagerDualNormal.java From apollo-DuerOS with Apache License 2.0 | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS_TRANSIENT"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); break; case AudioManager.AUDIOFOCUS_GAIN: LogUtil.d(TAG, "VR AUDIOFOCUS_GAIN"); break; default: break; } }
Example 3
Source File: AudioPlayerService.java From Dashchan with Apache License 2.0 | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: { pause(true); break; } case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: { boolean playing = mediaPlayer.isPlaying(); pause(false); if (playing) { pausedByTransientLossOfFocus = true; } break; } case AudioManager.AUDIOFOCUS_GAIN: { if (pausedByTransientLossOfFocus) { play(false); } break; } } }
Example 4
Source File: MediaSnippetsActivity.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
public void onAudioFocusChange(int focusChange) { AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); switch (focusChange) { case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK): // Lower the volume while ducking. mediaPlayer.setVolume(0.2f, 0.2f); break; case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT): mediaPlayer.pause(); break; case (AudioManager.AUDIOFOCUS_LOSS): mediaPlayer.stop(); am.abandonAudioFocus(this); break; case (AudioManager.AUDIOFOCUS_GAIN): // Return the volume to normal and resume if paused. mediaPlayer.setVolume(1f, 1f); mediaPlayer.start(); break; default: break; } }
Example 5
Source File: MediaPlaybackService.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
public void onAudioFocusChange(int focusChange) { AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); switch (focusChange) { case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) : // Lower the volume while ducking. mediaPlayer.setVolume(0.2f, 0.2f); break; case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) : mediaPlayer.pause(); break; case (AudioManager.AUDIOFOCUS_LOSS) : mediaPlayer.stop(); am.abandonAudioFocus(this); break; case (AudioManager.AUDIOFOCUS_GAIN) : // Return the volume to normal and resume if paused. mediaPlayer.setVolume(1f, 1f); mediaPlayer.start(); break; default: break; } updatePlaybackState(); }
Example 6
Source File: AudioFocusHelper.java From chromadoze with GNU General Public License v3.0 | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: // For example, a music player or a sleep timer stealing focus. NoiseService.stopNow(mContext, R.string.stop_reason_audiofocus); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // For example, an alarm or phone call. mVolumeListener.setDuckLevel(SampleShuffler.VolumeListener.DuckLevel.SILENT); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // For example, an email notification. mVolumeListener.setDuckLevel(SampleShuffler.VolumeListener.DuckLevel.DUCK); break; case AudioManager.AUDIOFOCUS_GAIN: // Resume the default volume level. mVolumeListener.setDuckLevel(SampleShuffler.VolumeListener.DuckLevel.NORMAL); break; } }
Example 7
Source File: AudioTrackManagerDualStreamType.java From apollo-DuerOS with Apache License 2.0 | 6 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: MsgHandlerCenter.dispatchMessage(CommonParams.VR_AUDIOFOCUS_LOSS); LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: MsgHandlerCenter.dispatchMessage(CommonParams.VR_AUDIOFOCUS_LOSS_TRANSIENT); LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS_TRANSIENT"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: MsgHandlerCenter.dispatchMessage(CommonParams.VR_AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK); LogUtil.d(TAG, "VR AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); break; case AudioManager.AUDIOFOCUS_GAIN: MsgHandlerCenter.dispatchMessage(CommonParams.VR_AUDIOFOCUS_GAIN); LogUtil.d(TAG, "VR AUDIOFOCUS_GAIN"); break; default: break; } }
Example 8
Source File: MusicService.java From Rey-MusicPlayer with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { try { stopPlaying(); mAudioManagerHelper.setHasAudioFocus(false); } catch (Exception e) { e.printStackTrace(); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { mAudioManagerHelper.setAudioDucked(true); mAudioManagerHelper.setTargetVolume(5); mAudioManagerHelper.setStepDownIncrement(1); mAudioManagerHelper.setCurrentVolume(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); mAudioManagerHelper.setOriginalVolume(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); mHandler.post(duckDownVolumeRunnable); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (mAudioManagerHelper.isAudioDucked()) { mAudioManagerHelper.setTargetVolume(mAudioManagerHelper.getOriginalVolume()); mAudioManagerHelper.setStepUpIncrement(1); mAudioManagerHelper.setCurrentVolume(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); mHandler.post(duckUpVolumeRunnable); mAudioManagerHelper.setAudioDucked(false); } else { mAudioManagerHelper.setHasAudioFocus(true); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { if (mMediaPlayer1 != null) { stopPlaying(); } mAudioManagerHelper.setHasAudioFocus(false); } }
Example 9
Source File: ModeService.java From apollo-DuerOS with Apache License 2.0 | 5 votes |
/** * 1. called when audio focus changed * <p> * 2. only change status of mIsMachinePause * * @param audioFocusStatus * * @return <p> * false:can resume/can not pause; * <p> * true:can pause/can not resume; */ public boolean getMode(int audioFocusStatus) { switch (audioFocusStatus) { // On HU side, VR and telephone may lead to audio focus loss transient, in this case HU will // actively pause the music playing back by sending Module Control command // and after that, HU need resume music playing when audio focus gained case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (getIsVRWorking()) { return false; } if (!getIsUserPause()) { // Set flag to indicate music paused by HU due to audio focus loss setIsMachinePause(true); return true; } else { return false; } case AudioManager.AUDIOFOCUS_GAIN: if (!getIsUserPause()) { if (getIsMachinePause()) { setIsMachinePause(false); return false; } } else { return true; } default: break; } return true; }
Example 10
Source File: AudioFocusHelper.java From DKVideoPlayer with Apache License 2.0 | 5 votes |
private void handleAudioFocusChange(int focusChange) { final VideoView videoView = mWeakVideoView.get(); if (videoView == null) { return; } switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN://获得焦点 case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT://暂时获得焦点 if (mStartRequested || mPausedForLoss) { videoView.start(); mStartRequested = false; mPausedForLoss = false; } if (!videoView.isMute())//恢复音量 videoView.setVolume(1.0f, 1.0f); break; case AudioManager.AUDIOFOCUS_LOSS://焦点丢失 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT://焦点暂时丢失 if (videoView.isPlaying()) { mPausedForLoss = true; videoView.pause(); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK://此时需降低音量 if (videoView.isPlaying() && !videoView.isMute()) { videoView.setVolume(0.1f, 0.1f); } break; } }
Example 11
Source File: PlayerMediaPlayer.java From blade-player with GNU General Public License v3.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { switch(focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if(playOnAudioFocus && !isPlaying()) play(); else if(isPlaying()) setVolume(MEDIA_VOLUME_DEFAULT, MEDIA_VOLUME_DEFAULT); playOnAudioFocus = PLAY_ON_AUDIOFOCUS; break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: //setVolume(MEDIA_VOLUME_DUCK, MEDIA_VOLUME_DUCK); /* we don't want to 'duck' for now, so take the same action as AUDIOFOCUS_LOSS_TRANSIENT */ if(isPlaying()) {playOnAudioFocus = true; pause();} break; /* We only lost audiofocus for a small ammount of time, relaunch player just after */ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if(isPlaying()) {playOnAudioFocus = true; pause();} break; /* We lost audiofocus definetely ; maybe another player was started or ... */ case AudioManager.AUDIOFOCUS_LOSS: if(isPlaying()) pause(); break; } }
Example 12
Source File: AudioFocusManager.java From HaoReader with GNU General Public License v3.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { // 重新获得焦点 case AudioManager.AUDIOFOCUS_GAIN: if (isPausedByFocusLossTransient) { // 通话结束,恢复播放 onFocusGainFromFocusLossTransient(); }else { // 恢复音量 onFocusGain(); } isPausedByFocusLossTransient = false; Logger.d(TAG, "重新获得焦点"); break; // 永久丢失焦点,如被其他播放器抢占 case AudioManager.AUDIOFOCUS_LOSS: onFocusLoss(); abandonAudioFocus(); Logger.d(TAG, "永久丢失焦点,如被其他播放器抢占"); break; // 短暂丢失焦点,如来电 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: onFocusLossTransient(); isPausedByFocusLossTransient = true; Logger.d(TAG, "短暂丢失焦点,如来电"); break; // 瞬间丢失焦点,如通知 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // 音量减小为一半 onFocusLossTransientCanDuck(); Logger.d(TAG, "瞬间丢失焦点,如通知"); break; default: break; } }
Example 13
Source File: LocalMusicPlayService.java From MusicPlus with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: if (mMediaPlayer == null){ return; }else if (!mMediaPlayer.isPlaying()) { mMediaPlayer.start(); } mMediaPlayer.setVolume(0.5f, 0.5f); break; case AudioManager.AUDIOFOCUS_LOSS: if (mMediaPlayer.isPlaying()) mMediaPlayer.stop(); mMediaPlayer.release(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: if (mMediaPlayer.isPlaying()) mMediaPlayer.pause(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (mMediaPlayer.isPlaying()) mMediaPlayer.setVolume(0.1f, 0.1f); break; } }
Example 14
Source File: LocalPlayback.java From LyricHere with Apache License 2.0 | 5 votes |
/** * Called by AudioManager on audio focus changes. * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener} */ @Override public void onAudioFocusChange(int focusChange) { LogUtils.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 == 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 { LogUtils.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange); } configMediaPlayerState(); }
Example 15
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 16
Source File: MainActivity.java From android-MediaRouter with Apache License 2.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { Log.d(TAG, "onAudioFocusChange: LOSS_TRANSIENT"); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_GAIN"); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { Log.d(TAG, "onAudioFocusChange: AUDIOFOCUS_LOSS"); } }
Example 17
Source File: MusicPlayer.java From iGap-Android with GNU Affero General Public License v3.0 | 5 votes |
@Override public void onAudioFocusChange(int focusChange) { latestAudioFocusState = focusChange; if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS) { pauseOnAudioFocusChange = true; if (mp != null && mp.isPlaying()) pauseSound(); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseOnAudioFocusChange) { pauseOnAudioFocusChange = false; //playSound(); // commented this line because after receive incoming call and end call, this listener will be called and sound will be played!!! } } }
Example 18
Source File: AudioFocusManager.java From YCAudioPlayer with Apache License 2.0 | 4 votes |
/** * 当音频焦点发生变化的时候调用这个方法,在这里可以处理逻辑 * 欢迎访问我的GitHub:https://github.com/yangchong211 * 如果可以的话,请star吧 * @param focusChange 焦点改变 */ @Override public void onAudioFocusChange(int focusChange) { int volume; switch (focusChange) { // 重新获得焦点 case AudioManager.AUDIOFOCUS_GAIN: if (!willPlay() && isPausedByFocusLossTransient) { // 通话结束,恢复播放 mPlayService.playPause(); } //获取音量 volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (mVolumeWhenFocusLossTransientCanDuck > 0 && volume == mVolumeWhenFocusLossTransientCanDuck / 2) { // 恢复音量 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeWhenFocusLossTransientCanDuck, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); } isPausedByFocusLossTransient = false; mVolumeWhenFocusLossTransientCanDuck = 0; break; // 永久丢失焦点,如被其他播放器抢占 case AudioManager.AUDIOFOCUS_LOSS: // 失去audio focus很长一段时间,必须停止所有的audio播放,清理资源 if (willPlay()) { forceStop(); } break; // 短暂丢失焦点,比如来了电话或者微信视频音频聊天等等 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // 暂时失去audio focus,但是很快就会重新获得,在此状态应该暂停所有音频播放,但是不能清除资源 if (willPlay()) { forceStop(); isPausedByFocusLossTransient = true; } break; // 瞬间丢失焦点,如通知 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // 暂时失去 audio focus,但是允许持续播放音频(以很小的声音),不需要完全停止播放。 // 音量减小为一半 volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (willPlay() && volume > 0) { mVolumeWhenFocusLossTransientCanDuck = volume; mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeWhenFocusLossTransientCanDuck / 2, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); } break; default: break; } }
Example 19
Source File: Audio.java From CodenameOne with GNU General Public License v2.0 | 4 votes |
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: // resume playback if (!isPlaying() && player != null && pausedDueToExternal) { player.start(); fireMediaStateChange(State.Playing); if(tempVolume > -1) { setVolume(tempVolume); tempVolume = -1; } } pausedDueToExternal = false; break; case AudioManager.AUDIOFOCUS_LOSS: // Lost focus for an unbounded amount of time: stop playback and release media player cleanup(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Log.d("CN1", "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 (isPlaying()) { pause(); pausedDueToExternal = true; fireMediaStateChange(State.Paused); } 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 (isPlaying()) { tempVolume = getVolume(); setVolume(10); } break; } }
Example 20
Source File: PlaybackService.java From VCL-Android with Apache License 2.0 | 4 votes |
@TargetApi(Build.VERSION_CODES.FROYO) private OnAudioFocusChangeListener createOnAudioFocusChangeListener() { return new OnAudioFocusChangeListener() { private boolean mLossTransient = false; private boolean mLossTransientCanDuck = false; @Override public void onAudioFocusChange(int focusChange) { /* * Pause playback during alerts and notifications */ switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: Log.i(TAG, "AUDIOFOCUS_LOSS"); // Stop playback changeAudioFocus(false); stop(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT"); // Pause playback if (mMediaPlayer.isPlaying()) { mLossTransient = true; mMediaPlayer.pause(); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); // Lower the volume if (mMediaPlayer.isPlaying()) { mMediaPlayer.setVolume(36); mLossTransientCanDuck = true; } break; case AudioManager.AUDIOFOCUS_GAIN: Log.i(TAG, "AUDIOFOCUS_GAIN: " + mLossTransientCanDuck + ", " + mLossTransient); // Resume playback if (mLossTransientCanDuck) { mMediaPlayer.setVolume(100); mLossTransientCanDuck = false; } if (mLossTransient) { mMediaPlayer.play(); mLossTransient = false; } break; } } }; }