[Android] [PATCH] Use callback for pauses instead of postDelayed

Edward Wang edward.c.wang at compdigitec.com
Sat Jan 10 03:30:01 CET 2015


---
 .../vlc/gui/video/VideoPlayerActivity.java         | 42 ++++++++++++----------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 4db04e9..bf22f8d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -262,6 +262,12 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private boolean mDisabledHardwareAcceleration = false;
     private int mPreviousHardwareAccelerationMode;
 
+    /**
+     * Flag to indicate whether the media should be paused once loaded
+     * (e.g. lock screen, or to restore the pause state)
+     */
+    private boolean mPauseOnLoaded = false;
+
     // Tips
     private View mOverlayTips;
     private static final String PREF_TIPS_SHOWN = "video_player_tips_shown";
@@ -631,6 +637,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     protected void onResume() {
         super.onResume();
         mSwitchingView = false;
+        mPauseOnLoaded = false;
         AudioServiceController.getInstance().bindAudioService(this,
                 new AudioServiceController.AudioServiceConnectionListener() {
                     @Override
@@ -669,23 +676,18 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     }
 
     private void startPlayback() {
-        loadMedia();
-
         /*
-         * if the activity has been paused by pressing the power button,
+         * If the activity has been paused by pressing the power button, then
          * pressing it again will show the lock screen.
-         * But onResume will also be called, even if vlc-android is still in the background.
-         * To workaround that, pause playback if the lockscreen is displayed
+         * But onResume will also be called, even if vlc-android is still in
+         * the background.
+         * To workaround this, pause playback if the lockscreen is displayed.
          */
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                if (mLibVLC != null && mLibVLC.isPlaying()) {
-                    KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
-                    if (km.inKeyguardRestrictedInputMode())
-                        mLibVLC.pause();
-                }
-            }}, 500);
+        KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
+        if(km.inKeyguardRestrictedInputMode())
+            mPauseOnLoaded = true;
+
+        loadMedia();
 
         // Add any selected subtitle file from the file picker
         if(mSubtitleSelectedFiles.size() > 0) {
@@ -1148,6 +1150,12 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                     break;
                 case EventHandler.MediaPlayerPlaying:
                     Log.i(TAG, "MediaPlayerPlaying");
+                    // Handle pause flag
+                    if(activity.mPauseOnLoaded) {
+                        activity.mPauseOnLoaded = false;
+                        activity.mLibVLC.pause();
+                        activity.setOverlayProgress();
+                    }
                     activity.stopLoadingAnimation();
                     activity.showOverlay();
                     /** FIXME: update the track list when it changes during the
@@ -2405,11 +2413,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             boolean wasPaused = mSettings.getBoolean(PreferencesActivity.VIDEO_PAUSED, false);
             if(wasPaused) {
                 Log.d(TAG, "Video was previously paused, resuming in paused mode");
-                mHandler.postDelayed(new Runnable() {
-                    @Override
-                    public void run() {
-                        mLibVLC.pause();
-                    }}, 500);
+                mPauseOnLoaded = true;
             }
 
             // Get possible subtitles
-- 
2.2.1



More information about the Android mailing list