[Android] Set slaves after playback start

Geoffrey Métais git at videolan.org
Fri Jun 8 11:12:17 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jun  8 10:54:42 2018 +0200| [25dc4fa7d21f61a8e8eb5773afef4e6062264675] | committer: Geoffrey Métais

Set slaves after playback start

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

 .../src/org/videolan/vlc/MediaSessionCallback.kt   |  4 +-
 .../src/org/videolan/vlc/PlaybackService.kt        |  3 +-
 .../src/org/videolan/vlc/media/PlayerController.kt |  5 +-
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 96 +++++++++++-----------
 4 files changed, 56 insertions(+), 52 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 78732b028..13ad2b026 100644
--- a/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -117,5 +117,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
 
     override fun onRewind() = playbackService.seek(Math.max(0, playbackService.time - 5000))
 
-    override fun onSkipToQueueItem(id: Long) = playbackService.playIndex(id.toInt())
+    override fun onSkipToQueueItem(id: Long) {
+        playbackService.playIndex(id.toInt())
+    }
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 27f425388..69320980a 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -44,6 +44,7 @@ import android.text.TextUtils
 import android.util.Log
 import android.widget.Toast
 import kotlinx.coroutines.experimental.CommonPool
+import kotlinx.coroutines.experimental.CoroutineStart
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
@@ -1002,7 +1003,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
      * @param flags LibVLC.MEDIA_* flags
      */
     @JvmOverloads
-    fun playIndex(index: Int, flags: Int = 0) = playlistManager.playIndex(index, flags)
+    fun playIndex(index: Int, flags: Int = 0) = launch(UI, CoroutineStart.UNDISPATCHED) { playlistManager.playIndex(index, flags) }
 
     @MainThread
     fun flush() {
diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 1877029e3..36f8fac05 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -15,6 +15,7 @@ import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.RendererDelegate
 import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.preferences.PreferencesActivity
+import org.videolan.vlc.util.VLCIO
 import org.videolan.vlc.util.VLCInstance
 import org.videolan.vlc.util.VLCOptions
 import kotlin.math.abs
@@ -185,8 +186,8 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         setPlaybackStopped()
     }
 
-    suspend fun setSlaves(media: Media, mw: MediaWrapper) {
-        val list = withContext(CommonPool) {
+    fun setSlaves(media: Media, mw: MediaWrapper) = launch(UI, CoroutineStart.UNDISPATCHED) {
+        val list = withContext(VLCIO) {
             mw.slaves?.let {
                 for (slave in it) media.addSlave(slave)
                 MediaDatabase.getInstance().saveSlaves(mw)
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 7b62c0632..f54f8e2d1 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -115,8 +115,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
         // Add handler after loading the list
         mediaList.addEventListener(this)
-        playIndex(position)
-        onPlaylistLoaded()
+        launch(UI, CoroutineStart.UNDISPATCHED) {
+            playIndex(position)
+            onPlaylistLoaded()
+        }
     }
 
     @Volatile
@@ -186,7 +188,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             return
         }
         videoBackground = !player.isVideoPlaying() && player.canSwitchToVideo()
-        playIndex(currentIndex)
+        launch(UI, CoroutineStart.UNDISPATCHED) { playIndex(currentIndex) }
     }
 
     fun stop(systemExit: Boolean = false) {
@@ -216,7 +218,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 player.stop()
                 return
             }
-            playIndex(currentIndex)
+            launch(UI, CoroutineStart.UNDISPATCHED) { playIndex(currentIndex) }
         } else player.setPosition(0F)
     }
 
@@ -240,7 +242,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         showAudioPlayer.value = player.playbackState != PlaybackStateCompat.STATE_STOPPED && (item !== null || !player.isVideoPlaying())
     }
 
-    fun playIndex(index: Int, flags: Int = 0) {
+    suspend fun playIndex(index: Int, flags: Int = 0) {
         if (mediaList.size() == 0) {
             Log.w(TAG, "Warning: empty media list, nothing to play !")
             return
@@ -260,55 +262,53 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         player.switchToVideo = false
         if (TextUtils.equals(mw.uri.scheme, "content")) MediaUtils.retrieveMediaTitle(mw)
 
-        launch(UI, CoroutineStart.UNDISPATCHED) {
-            if (mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO) && player.getAudioTracksCount() == 0) {
-                determinePrevAndNextIndices(true)
+        if (mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO) && player.getAudioTracksCount() == 0) {
+            determinePrevAndNextIndices(true)
+            skipMedia()
+        } else if (mw.type != MediaWrapper.TYPE_VIDEO || isVideoPlaying || player.hasRenderer
+                || mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO)) {
+            val uri = FileUtils.getUri(mw.uri)
+            if (uri == null) {
                 skipMedia()
-            } else if (mw.type != MediaWrapper.TYPE_VIDEO || isVideoPlaying || player.hasRenderer
-                    || mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO)) {
-                val uri = FileUtils.getUri(mw.uri)
-                if (uri == null) {
-                    skipMedia()
-                    return at launch
-                }
-                val media = Media(VLCInstance.get(), uri)
-                setStartTime(media, mw)
-                VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
-                /* keeping only video during benchmark */
-                if (isBenchmark) {
-                    media.addOption(":no-audio")
-                    media.addOption(":no-spu")
-                    if (isHardware) {
-                        media.addOption(":codec=mediacodec_ndk,mediacodec_jni,none")
-                        isHardware = false
-                    }
+                return
+            }
+            val media = Media(VLCInstance.get(), uri)
+            setStartTime(media, mw)
+            VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
+            /* keeping only video during benchmark */
+            if (isBenchmark) {
+                media.addOption(":no-audio")
+                media.addOption(":no-spu")
+                if (isHardware) {
+                    media.addOption(":codec=mediacodec_ndk,mediacodec_jni,none")
+                    isHardware = false
                 }
-                media.setEventListener(this at PlaylistManager)
-                player.setSlaves(media, mw)
-                player.startPlayback(media, mediaplayerEventListener)
-                media.release()
-                determinePrevAndNextIndices()
-                service.onNewPlayback()
-                if (settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)) launch {
-                    var id = mw.id
-                    if (id == 0L) {
-                        var internalMedia = medialibrary.findMedia(mw)
-                        if (internalMedia != null && internalMedia.id != 0L)
+            }
+            media.setEventListener(this at PlaylistManager)
+            player.setSlaves(media, mw)
+            player.startPlayback(media, mediaplayerEventListener)
+            media.release()
+            newMedia = true
+            determinePrevAndNextIndices()
+            service.onNewPlayback()
+            if (settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)) launch {
+                var id = mw.id
+                if (id == 0L) {
+                    var internalMedia = medialibrary.findMedia(mw)
+                    if (internalMedia != null && internalMedia.id != 0L)
+                        id = internalMedia.id
+                    else {
+                        internalMedia = medialibrary.addMedia(Uri.decode(mw.uri.toString()))
+                        if (internalMedia != null)
                             id = internalMedia.id
-                        else {
-                            internalMedia = medialibrary.addMedia(Uri.decode(mw.uri.toString()))
-                            if (internalMedia != null)
-                                id = internalMedia.id
-                        }
                     }
-                    medialibrary.increasePlayCount(id)
                 }
-                saveCurrentMedia()
-                newMedia = true
-            } else { //Start VideoPlayer for first video, it will trigger playIndex when ready.
-                player.stop()
-                VideoPlayerActivity.startOpened(ctx, mw.uri, currentIndex)
+                medialibrary.increasePlayCount(id)
             }
+            saveCurrentMedia()
+        } else { //Start VideoPlayer for first video, it will trigger playIndex when ready.
+            player.stop()
+            VideoPlayerActivity.startOpened(ctx, mw.uri, currentIndex)
         }
     }
 



More information about the Android mailing list