[Android] Set media start-time instead of seeking

Geoffrey Métais git at videolan.org
Wed Apr 4 18:35:43 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar 21 13:54:44 2018 +0100| [f306d181f6f64d8354979a00c166706898118ae2] | committer: Geoffrey Métais

Set media start-time instead of seeking

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

 .../videolan/medialibrary/media/MediaWrapper.java  |  8 +++---
 .../src/org/videolan/vlc/PlaybackService.java      |  2 +-
 .../vlc/gui/video/VideoPlayerActivity.java         |  6 ++---
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 30 ++++++++--------------
 4 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java b/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
index 485538073..52272e1c7 100644
--- a/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
+++ b/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
@@ -404,10 +404,10 @@ public class MediaWrapper extends MediaLibraryItem implements Parcelable {
     }
 
     public boolean isPodcast() {
-        return mType == TYPE_AUDIO && (TextUtils.isEmpty(mAlbum) && mLength > PODCAST_THRESHOLD)
-                || ("podcast".equalsIgnoreCase(mGenre))
-                || ("audiobooks".equalsIgnoreCase(mGenre))
-                || ("audiobook".equalsIgnoreCase(mGenre));
+        return mType == TYPE_AUDIO && (TextUtils.isEmpty(mAlbum) && mLength > PODCAST_THRESHOLD
+                || "podcast".equalsIgnoreCase(mGenre)
+                || "audiobooks".equalsIgnoreCase(mGenre)
+                || "audiobook".equalsIgnoreCase(mGenre));
     }
 
     public void setType(int type){
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 28a608688..768f5774d 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1689,7 +1689,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
     }
 
     @MainThread
-    public void saveTimeToSeek(long time) {
+    public void saveStartTime(long time) {
         playlistManager.setSavedTime(time);
     }
 
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 77dd65d7d..c664ca088 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -3154,10 +3154,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
 
             // Set resume point
             if (!continueplayback) {
-                if (!fromStart && savedTime <= 0L && media.getTime() > 0L)
-                    savedTime = media.getTime();
-                if (savedTime > 0L)
-                    mService.saveTimeToSeek(savedTime);
+                if (!fromStart && savedTime <= 0L && media.getTime() > 0L) savedTime = media.getTime();
+                if (savedTime > 0L) mService.saveStartTime(savedTime);
             }
 
             // Handle playback
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index d637c5e64..100d09792 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -10,10 +10,8 @@ import android.support.v7.preference.PreferenceManager
 import android.text.TextUtils
 import android.util.Log
 import android.widget.Toast
-import kotlinx.coroutines.experimental.CoroutineStart
+import kotlinx.coroutines.experimental.*
 import kotlinx.coroutines.experimental.android.UI
-import kotlinx.coroutines.experimental.async
-import kotlinx.coroutines.experimental.launch
 import org.videolan.libvlc.Media
 import org.videolan.libvlc.MediaPlayer
 import org.videolan.libvlc.RendererItem
@@ -262,6 +260,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             } else if (mw.type != MediaWrapper.TYPE_VIDEO || isVideoPlaying || player.hasRenderer
                     || mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO)) {
                 val media = Media(VLCInstance.get(), FileUtils.getUri(mw.uri))
+                setStartTime(media, mw)
                 VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
                 /* keeping only video during benchmark */
                 if (isBenchmark) {
@@ -280,7 +279,6 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 player.setSlaves(mw)
                 player.startPlayback(media, mediaplayerEventListener)
                 media.release()
-                if (savedTime <= 0L && mw.time >= 0L && mw.isPodcast) savedTime = mw.time
                 determinePrevAndNextIndices()
                 service.onNewPlayback(mw)
                 if (settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)) launch {
@@ -546,21 +544,16 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
     fun getMedia(position: Int) = mediaList.getMedia(position)
 
-    private fun seekToResume(media: MediaWrapper) {
-        var mw = media
-        if (savedTime > 0L) {
-            if (savedTime < 0.95 * player.length) player.seek(savedTime)
-            savedTime = 0L
-        } else {
-            val length = player.length
-            if (mw.length <= 0L && length > 0L) {
-                mw = medialibrary.findMedia(mw)
-                if (mw.id != 0L) {
-                    mw.time = mw.getMetaLong(MediaWrapper.META_PROGRESS)
-                    if (mw.time > 0L) player.seek(mw.time)
-                }
-            }
+    private suspend fun setStartTime(media: Media, mw: MediaWrapper) {
+        if (savedTime <= 0L ) {
+            if (mw.time >= 0L) savedTime = mw.time
+            else if (mw.time == 0L && mw.type == MediaWrapper.TYPE_VIDEO || mw.isPodcast) savedTime = withContext(CommonPool) { medialibrary.findMedia(mw).getMetaLong(MediaWrapper.META_PROGRESS) }
+
         }
+        if (savedTime <= 0L) return
+        val mediaLength = mw.length
+        if (mediaLength > 0 && savedTime < 0.95 * mediaLength) media.addOption(":start-time=${savedTime/1000L}")
+        savedTime = 0L
     }
 
     @Synchronized
@@ -661,7 +654,6 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 videoBackground = false
                 val mw = medialibrary.findMedia(getCurrentMedia())
                 if (newMedia) {
-                    seekToResume(mw)
                     loadMediaMeta(mw)
                     if (mw.type == MediaWrapper.TYPE_STREAM) medialibrary.addToHistory(mw.location, mw.title)
                     saveMediaList()



More information about the Android mailing list