[Android] VideoPlayer: save media index when leaving app.
Duncan McNamara
git at videolan.org
Tue Oct 18 12:08:33 UTC 2022
vlc-android | branch: master | Duncan McNamara <dcn.mcnamara at gmail.com> | Mon Oct 17 15:19:33 2022 +0200| [72ed253419de5be64ca244e5d932920fa04567a2] | committer: Nicolas Pomepuy
VideoPlayer: save media index when leaving app.
On some low performance devices, when leaving the app, the paused
service will be killed, and respawned when the app is restarted.
As the medialist was saved but not the index, resuming would go to
the 1st media in the saved medialist. This saves the media index
also.
Fixes #2705
> https://code.videolan.org/videolan/vlc-android/commit/72ed253419de5be64ca244e5d932920fa04567a2
---
application/vlc-android/src/org/videolan/vlc/PlaybackService.kt | 6 +++---
.../src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt | 7 ++++++-
.../vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt | 4 ++--
3 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index bcd670202..ec3ea36fb 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -1506,11 +1506,11 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
*/
@MainThread
- fun append(mediaList: Array<MediaWrapper>) = append(mediaList.toList())
+ fun append(mediaList: Array<MediaWrapper>, index: Int = 0) = append(mediaList.toList(), index)
@MainThread
- fun append(mediaList: List<MediaWrapper>) = lifecycleScope.launch(start = CoroutineStart.UNDISPATCHED) {
- playlistManager.append(mediaList)
+ fun append(mediaList: List<MediaWrapper>, index: Int = 0) = lifecycleScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ playlistManager.append(mediaList, index)
onMediaListChanged()
}
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index cea257c37..30a2a92ea 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -132,6 +132,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
var rootView: View? = null
var videoUri: Uri? = null
var savedMediaList: ArrayList<MediaWrapper>? = null
+ var savedMediaIndex: Int = 0
private var askResume = true
var playlistModel: PlaylistModel? = null
@@ -489,6 +490,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
if (savedInstanceState != null) {
savedTime = savedInstanceState.getLong(KEY_TIME)
savedMediaList = savedInstanceState.getParcelableArrayList(KEY_MEDIA_LIST)
+ savedMediaIndex = savedInstanceState.getInt(KEY_MEDIA_INDEX)
val list = savedInstanceState.getBoolean(KEY_LIST, false)
if (list) {
intent.removeExtra(PLAY_EXTRA_ITEM_LOCATION)
@@ -662,8 +664,10 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
if (playlistModel == null) outState.putParcelable(KEY_URI, videoUri)
}
val mediaList = service?.playlistManager?.getMediaList() ?: savedMediaList
+ val mediaIndex = service?.playlistManager?.currentIndex ?: 0
if (mediaList != null) {
outState.putParcelableArrayList(KEY_MEDIA_LIST, ArrayList(mediaList))
+ outState.putInt(KEY_MEDIA_INDEX, mediaIndex)
savedMediaList = null
}
videoUri = null
@@ -2240,7 +2244,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
if (service != null) {
this.service = service
if (savedMediaList != null && service.currentMediaWrapper == null) {
- service.append(savedMediaList!!)
+ service.append(savedMediaList!!, savedMediaIndex)
savedMediaList = null
}
//We may not have the permission to access files
@@ -2296,6 +2300,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
private const val KEY_TIME = "saved_time"
private const val KEY_LIST = "saved_list"
private const val KEY_MEDIA_LIST = "media_list"
+ private const val KEY_MEDIA_INDEX = "media_index"
private const val KEY_URI = "saved_uri"
const val OVERLAY_INFINITE = -1
const val FADE_OUT = 1
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index c6efea5f4..d63a68e8a 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -846,10 +846,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
* Append to the current existing playlist
*/
@MainThread
- suspend fun append(list: List<MediaWrapper>) {
+ suspend fun append(list: List<MediaWrapper>, index: Int = 0) {
if (BuildConfig.BETA) Log.d(TAG, "append with values: ", Exception("Call stack"))
if (!hasCurrentMedia()) {
- launch { load(list, 0, mlUpdate = true) }
+ launch { load(list, index, mlUpdate = true) }
return
}
val list = withContext(Dispatchers.IO) { list.updateWithMLMeta() }
More information about the Android
mailing list