diff options
| author | zdi <zdi@codeaurora.org> | 2016-01-22 10:07:41 +0800 |
|---|---|---|
| committer | Steve Kondik <steve@cyngn.com> | 2016-09-26 04:54:47 -0700 |
| commit | 4c09417abd211b997122203a1f37bed8a9b848bc (patch) | |
| tree | 605066b759c7ae4e6a4a3c38db3b5ff9b8c8e2b8 | |
| parent | 67b1d3cedf537b37303250c2d5893b75b4b98805 (diff) | |
| download | android_packages_apps_Gallery2-4c09417abd211b997122203a1f37bed8a9b848bc.tar.gz android_packages_apps_Gallery2-4c09417abd211b997122203a1f37bed8a9b848bc.tar.bz2 android_packages_apps_Gallery2-4c09417abd211b997122203a1f37bed8a9b848bc.zip | |
Gallery2: Auto pause and replay video when audio focus changed.
Add OnAudioFocusChangeListener in CodeauroraVideoView, pause video when
lose audio focus and replay video when gain audio focus.
Change-Id: Id8e3e4b0afbc72892301a08fb8a122c285fbfe97
CRs-Fixed: 962677
| -rwxr-xr-x | src/com/android/gallery3d/app/MoviePlayer.java | 54 | ||||
| -rwxr-xr-x | src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java | 12 |
2 files changed, 56 insertions, 10 deletions
diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java index 94d383295..9536190d1 100755 --- a/src/com/android/gallery3d/app/MoviePlayer.java +++ b/src/com/android/gallery3d/app/MoviePlayer.java @@ -139,6 +139,9 @@ public class MoviePlayer implements // If the time bar is visible. private boolean mShowing; + // used to track what type of audio focus loss caused the video to pause + private boolean mPausedByTransientLossOfFocus = false; + private Virtualizer mVirtualizer; private MovieActivity mActivityContext;//for dialog and toast context @@ -220,6 +223,28 @@ public class MoviePlayer implements } }; + private AudioManager.OnAudioFocusChangeListener mAudioFocusListener = + new AudioManager.OnAudioFocusChangeListener() { + public void onAudioFocusChange(int focusChange) { + switch (focusChange) { + case AudioManager.AUDIOFOCUS_GAIN: + if (mPausedByTransientLossOfFocus) { + onPlayVideo(); + mPausedByTransientLossOfFocus = false; + } + break; + case AudioManager.AUDIOFOCUS_LOSS: + case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: + if (mVideoView != null && mVideoView.isPlaying()) { + onPauseVideo(); + mPausedByTransientLossOfFocus = true; + } + break; + default: + } + } + }; + public MoviePlayer(View rootView, final MovieActivity movieActivity, IMovieItem info, Bundle savedInstance, boolean canReplay) { mContext = movieActivity.getApplicationContext(); @@ -236,6 +261,7 @@ public class MoviePlayer implements mVideoView.setOnErrorListener(this); mVideoView.setOnCompletionListener(this); + mVideoView.setOnAudioFocusChangeListener(mAudioFocusListener); if (mVirtualizer != null) { mVirtualizer.release(); @@ -686,17 +712,25 @@ public class MoviePlayer implements @Override public void onPlayPause() { if (mVideoView.isPlaying()) { - if (mVideoView.canPause()) { - pauseVideo(); - //set view disabled(play/pause asynchronous processing) - mController.setViewEnabled(true); - if (mControllerRewindAndForwardExt != null) { - mControllerRewindAndForwardExt.showControllerButtonsView(mPlayerExt - .canStop(), false, false); - } - } + onPauseVideo(); } else { - playVideo(); + onPlayVideo(); + } + } + + private void onPlayVideo() { + playVideo(); + //set view disabled(play/pause asynchronous processing) + mController.setViewEnabled(true); + if (mControllerRewindAndForwardExt != null) { + mControllerRewindAndForwardExt.showControllerButtonsView(mPlayerExt + .canStop(), false, false); + } + } + + private void onPauseVideo() { + if (mVideoView.canPause()) { + pauseVideo(); //set view disabled(play/pause asynchronous processing) mController.setViewEnabled(true); if (mControllerRewindAndForwardExt != null) { diff --git a/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java b/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java index 6d01b8adb..696371db9 100755 --- a/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java +++ b/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java @@ -98,6 +98,7 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr private boolean mIsShowDialog = false; private boolean mErrorDialogShowing = false; private KeyguardManager mKeyguardManager; + private AudioManager.OnAudioFocusChangeListener mAudioFocusListener; private final Handler mHandler = new Handler() { public void handleMessage(final Message msg) { @@ -454,6 +455,8 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mMediaPlayer = null; mCurrentState = STATE_IDLE; mTargetState = STATE_IDLE; + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.abandonAudioFocus(null); } } @@ -472,6 +475,9 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); + try { mMediaPlayer = new MediaPlayer(); if (mAudioSession != 0) { @@ -621,6 +627,10 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mOnInfoListener = l; } + public void setOnAudioFocusChangeListener(AudioManager.OnAudioFocusChangeListener l) { + mAudioFocusListener = l; + } + SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() { public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { @@ -693,6 +703,8 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr if (cleartargetstate) { mTargetState = STATE_IDLE; } + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.abandonAudioFocus(null); } } |
