[Android] VideoPlayerActivity: refactor stopPlayback

Thomas Guillem git at videolan.org
Wed Nov 2 11:46:34 CET 2016


vlc-android | branch: master | Thomas Guillem <thomas at gllm.fr> | Fri Oct 28 18:55:09 2016 +0200| [3583cf1929da845ab4e43a826e08df12ceceee61] | committer: Thomas Guillem

VideoPlayerActivity: refactor stopPlayback

Save playback time, rate and subtitles from onStop().

> https://code.videolan.org/videolan/vlc-android/commit/3583cf1929da845ab4e43a826e08df12ceceee61
---

 .../vlc/gui/video/VideoPlayerActivity.java         | 88 ++++++++++++----------
 1 file changed, 47 insertions(+), 41 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 01274bf..94411c0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -268,6 +268,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
     private int mOverlayTimeout = 0;
     private boolean mLockBackButton = false;
     boolean mWasPaused = false;
+    private long mSavedTime = -1;
+    private float mSavedRate = 1.f;
 
     /**
      * For uninterrupted switching between audio and video mode
@@ -677,8 +679,39 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
                 mSettings.getBoolean(PreferencesActivity.VIDEO_BACKGROUND, false)) {
             switchToAudioMode(false);
         }
+
         stopPlayback();
 
+        SharedPreferences.Editor editor = mSettings.edit();
+        if (mSavedTime != -1) {
+            // Save position
+            if(MediaDatabase.getInstance().mediaItemExists(mUri)) {
+                MediaDatabase.getInstance().updateMedia(
+                        mUri,
+                        MediaDatabase.INDEX_MEDIA_TIME,
+                        mSavedTime);
+            } else {
+                // Video file not in media library, store time just for onResume()
+                editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, mSavedTime);
+            }
+        }
+
+        editor.putFloat(PreferencesActivity.VIDEO_RATE, mSavedRate);
+
+        // Save selected subtitles
+        String subtitleList_serialized = null;
+        if(mSubtitleSelectedFiles.size() > 0) {
+            Log.d(TAG, "Saving selected subtitle files");
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            try {
+                ObjectOutputStream oos = new ObjectOutputStream(bos);
+                oos.writeObject(mSubtitleSelectedFiles);
+                subtitleList_serialized = bos.toString();
+            } catch(IOException e) {}
+        }
+        editor.putString(PreferencesActivity.VIDEO_SUBTITLE_FILES, subtitleList_serialized);
+        editor.apply();
+
         restoreBrightness();
 
         if (mSubtitlesGetTask != null)
@@ -747,6 +780,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
         if (mPlaybackStarted || mService == null)
             return;
 
+        mSavedRate = 1.0f;
+        mSavedTime = -1;
         mPlaybackStarted = true;
 
         final IVLCVout vlcVout = mService.getVLCVout();
@@ -881,54 +916,25 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
                 mService.showWithoutParse(mService.getCurrentMediaPosition());
             return;
         }
-        if (!mWasPaused)
-            mService.pause();
 
-        cleanUI();
 
-        long time = getTime();
-        long length = mService.getLength();
-        //remove saved position if in the last 5 seconds
-        if (length - time < 5000)
-            time = 0;
-        else
-            time -= 2000; // go back 2 seconds, to compensate loading time
+        cleanUI();
 
-        SharedPreferences.Editor editor = mSettings.edit();
-        // Save position
         if (mService.isSeekable()) {
-            if(MediaDatabase.getInstance().mediaItemExists(mUri)) {
-                MediaDatabase.getInstance().updateMedia(
-                        mUri,
-                        MediaDatabase.INDEX_MEDIA_TIME,
-                        time);
-            } else {
-                // Video file not in media library, store time just for onResume()
-                editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, time);
-            }
+            mSavedTime = getTime();
+            long length = mService.getLength();
+            //remove saved position if in the last 5 seconds
+            if (length - mSavedTime < 5000)
+                mSavedTime = 0;
+            else
+                mSavedTime -= 2000; // go back 2 seconds, to compensate loading time
         }
 
-        editor.putFloat(PreferencesActivity.VIDEO_RATE, mService.getRate());
-        mRateHasChanged = mService.getRate() != 1.0f;
-
-        if (isFinishing()) {
-            // Save selected subtitles
-            String subtitleList_serialized = null;
-            if(mSubtitleSelectedFiles.size() > 0) {
-                Log.d(TAG, "Saving selected subtitle files");
-                ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                try {
-                    ObjectOutputStream oos = new ObjectOutputStream(bos);
-                    oos.writeObject(mSubtitleSelectedFiles);
-                    subtitleList_serialized = bos.toString();
-                } catch(IOException e) {}
-            }
-            editor.putString(PreferencesActivity.VIDEO_SUBTITLE_FILES, subtitleList_serialized);
+        mSavedRate = mService.getRate();
+        mRateHasChanged = mSavedRate != 1.0f;
 
-            mService.setRate(1.0f, false);
-            mService.stop();
-        }
-        editor.apply();
+        mService.setRate(1.0f, false);
+        mService.stop();
     }
 
     private void cleanUI() {



More information about the Android mailing list