[Android] [PATCH] Option to ask for resume video or play from start

Geoffrey Métais geoffrey.metais at gmail.com
Thu Apr 2 13:55:21 CEST 2015


---
 vlc-android/res/values/strings.xml                 |  4 +
 vlc-android/res/xml/preferences.xml                |  6 ++
 .../vlc/gui/video/VideoPlayerActivity.java         | 93 +++++++++++++++++-----
 3 files changed, 84 insertions(+), 19 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 5f23d6f..2888f36 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -329,6 +329,10 @@
     <string name="playlist_save">Save Playlist</string>
     <string name="playlist_name_hint">playlist name</string>
     <string name="go_to_chapter">Go to chapter…</string>
+    <string name="resume_from_position">Resume from last position</string>
+    <string name="confirm_resume">Resume from last position?</string>
+    <string name="confirm_resume_title">Ask confirmation to resume</string>
+    <string name="confirm_resume_summary">if activated you will be asked to confirm when a video can be resumed from last position</string>
 
     <string-array name="hardware_acceleration_list">
         <item>@string/automatic</item>
diff --git a/vlc-android/res/xml/preferences.xml b/vlc-android/res/xml/preferences.xml
index dd0283a..1136f6f 100644
--- a/vlc-android/res/xml/preferences.xml
+++ b/vlc-android/res/xml/preferences.xml
@@ -53,6 +53,12 @@
             <PreferenceCategory android:title="@string/interface_other_category" >
                 <CheckBoxPreference
                     android:defaultValue="false"
+                    android:key="dialog_confirm_resume"
+                    android:summary="@string/confirm_resume_summary"
+                    android:widgetLayout="@layout/custom_chexbox"
+                    android:title="@string/confirm_resume_title" />
+                <CheckBoxPreference
+                    android:defaultValue="false"
                     android:key="force_list_portrait"
                     android:summary="@string/force_list_portrait_summary"
                     android:widgetLayout="@layout/custom_chexbox"
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 a0ddf2c..1d91fa6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -21,7 +21,9 @@
 package org.videolan.vlc.gui.video;
 
 import android.annotation.TargetApi;
+import android.app.Activity;
 import android.app.AlertDialog;
+import android.app.Dialog;
 import android.app.KeyguardManager;
 import android.app.Presentation;
 import android.content.ActivityNotFoundException;
@@ -52,6 +54,7 @@ import android.preference.PreferenceManager;
 import android.provider.MediaStore;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
+import android.support.v4.app.DialogFragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.view.GestureDetectorCompat;
 import android.support.v4.view.MenuItemCompat;
@@ -158,6 +161,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private LibVLC mLibVLC;
     private MediaWrapperListPlayer mMediaListPlayer;
     private String mLocation;
+    private boolean mAskResume = true;
     private GestureDetectorCompat mDetector;
 
     private static final int SURFACE_BEST_FIT = 0;
@@ -448,6 +452,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         mHardwareAccelerationError = false;
         mEndReached = false;
 
+        mAskResume = mSettings.getBoolean("dialog_confirm_resume", false);
         // Clear the resume time, since it is only used for resumes in external
         // videos.
         SharedPreferences.Editor editor = mSettings.edit();
@@ -2467,6 +2472,16 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         mSurfaceView.setKeepScreenOn(false);
     }
 
+    /*
+     * Additionnal method to prevent alert dialog to pop up
+     */
+    @SuppressWarnings({ "unchecked" })
+    private void loadMedia(boolean fromStart) {
+        mAskResume = false;
+        getIntent().putExtra(PLAY_EXTRA_FROM_START, fromStart);
+        loadMedia();
+    }
+
     /**
      * External extras:
      * - position (long) - position of the video to start with (in ms)
@@ -2594,6 +2609,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             fromStart = getIntent().getExtras().getBoolean(PLAY_EXTRA_FROM_START);
             if (getIntent().hasExtra(PLAY_EXTRA_SUBTITLES_LOCATION))
                 mSubtitleSelectedFiles.add(getIntent().getExtras().getString(PLAY_EXTRA_SUBTITLES_LOCATION));
+            mAskResume &= !fromStart;
         }
 
         /* WARNING: hack to avoid a crash in mediacodec on KitKat.
@@ -2611,19 +2627,15 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             }
         }
 
-        /* Start / resume playback */
-        if (savedIndexPosition > -1) {
-            AudioServiceController.getInstance().stop(); // Stop the previous playback.
-            mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
-        } else if (mLocation != null && mLocation.length() > 0) {
-            AudioServiceController.getInstance().stop(); // Stop the previous playback.
+        /* prepare playback */
+        AudioServiceController.getInstance().stop(); // Stop the previous playback.
+        if (savedIndexPosition == -1 && mLocation != null && mLocation.length() > 0) {
             mMediaListPlayer.getMediaList().clear();
             final Media media = new Media(mLibVLC, mLocation);
             media.parse(); // FIXME: parse should'nt be done asynchronously
             media.release();
             mMediaListPlayer.getMediaList().add(new MediaWrapper(media));
             savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
-            mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
         }
         mCanSeek = false;
 
@@ -2632,8 +2644,16 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             MediaWrapper media = MediaDatabase.getInstance().getMedia(mLocation);
             if(media != null) {
                 // in media library
-                if(media.getTime() > 0 && !fromStart)
-                    seek(media.getTime(), media.getLength());
+                if(media.getTime() > 0 && !fromStart) {
+                    if (mAskResume) {
+                        showConfirmResumeDialog();
+                        return;
+                    } else {
+                        mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
+                        seek(media.getTime(), media.getLength());
+                    }
+                } else
+                    mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
                 // Consume fromStart option after first use to prevent
                 // restarting again when playback is paused.
                 getIntent().putExtra(PLAY_EXTRA_FROM_START, false);
@@ -2642,17 +2662,31 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 mLastSpuTrack = media.getSpuTrack();
             } else {
                 // not in media library
-                long rTime = mSettings.getLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
-                if (rTime > 0) {
-                    Editor editor = mSettings.edit();
-                    editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
-                    Util.commitPreferences(editor);
-                }
 
-                if (intentPosition > 0)
-                    seek(intentPosition);
-                else if (rTime > 0)
-                        seek(rTime);
+                if (intentPosition > 0 && !fromStart) {
+                    if (mAskResume) {
+                        showConfirmResumeDialog();
+                        return;
+                    } else {
+                        mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
+                        seek(intentPosition);
+                    }
+                } else {
+                    long rTime = mSettings.getLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
+                    if (rTime > 0 && !fromStart) {
+                        if (mAskResume) {
+                            showConfirmResumeDialog();
+                            return;
+                        } else {
+                            Editor editor = mSettings.edit();
+                            editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
+                            Util.commitPreferences(editor);
+                            mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
+                            seek(rTime);
+                        }
+                    } else
+                        mMediaListPlayer.playIndex(savedIndexPosition, wasPaused);
+                }
             }
 
             // Get possible subtitles
@@ -2764,6 +2798,27 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         }
     }
 
+    public void showConfirmResumeDialog() {
+        if (isFinishing())
+            return;
+        pause();
+        /* Encountered Error, exit player with a message */
+        mAlertDialog = new AlertDialog.Builder(VideoPlayerActivity.this)
+                .setMessage(R.string.confirm_resume)
+                .setPositiveButton(R.string.resume_from_position, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        loadMedia(false);
+                    }
+                })
+                .setNegativeButton(R.string.play_from_start, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        loadMedia(true);
+                    }
+                })
+                .create();
+        mAlertDialog.show();
+    }
+
     public void showAdvancedOptions(View v) {
         FragmentManager fm = getSupportFragmentManager();
         AdvOptionsDialog advOptionsDialog = new AdvOptionsDialog();
-- 
2.1.0



More information about the Android mailing list