Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 841ab275 authored by zdi's avatar zdi
Browse files

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
parent f4c20173
Loading
Loading
Loading
Loading
+44 −10
Original line number Diff line number Diff line
@@ -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
@@ -218,6 +221,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();
@@ -234,6 +259,7 @@ public class MoviePlayer implements

        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnAudioFocusChangeListener(mAudioFocusListener);

        if (mVirtualizer != null) {
            mVirtualizer.release();
@@ -684,8 +710,14 @@ public class MoviePlayer implements
    @Override
    public void onPlayPause() {
        if (mVideoView.isPlaying()) {
            if (mVideoView.canPause()) {
                pauseVideo();
            onPauseVideo();
        } else {
            onPlayVideo();
        }
    }

    private void onPlayVideo() {
        playVideo();
        //set view disabled(play/pause asynchronous processing)
        mController.setViewEnabled(true);
        if (mControllerRewindAndForwardExt != null) {
@@ -693,8 +725,10 @@ public class MoviePlayer implements
                    .canStop(), false, false);
        }
    }
        } else {
            playVideo();

    private void onPauseVideo() {
        if (mVideoView.canPause()) {
            pauseVideo();
            //set view disabled(play/pause asynchronous processing)
            mController.setViewEnabled(true);
            if (mControllerRewindAndForwardExt != null) {
+12 −0
Original line number Diff line number Diff line
@@ -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) {
@@ -449,6 +450,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);
        }
    }

@@ -467,6 +470,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) {
@@ -616,6 +622,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) {
@@ -681,6 +691,8 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr
            if (cleartargetstate) {
                mTargetState  = STATE_IDLE;
            }
            AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            am.abandonAudioFocus(null);
        }
    }