[Android] Ensure mediasession and notif channels are ready

Geoffrey Métais git at videolan.org
Thu Oct 17 12:09:17 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct 17 11:48:03 2019 +0200| [90ad3e69af5e63f659eccee42cdba6c69ab69d97] | committer: Geoffrey Métais

Ensure mediasession and notif channels are ready

(cherry picked from commit b2d59fbfe30546ae2f48f9c350fa2f799465fcff)

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

 vlc-android/src/org/videolan/vlc/PlaybackService.kt | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 55181b882..44171e98f 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -469,7 +469,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
         wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG)
 
         updateHasWidget()
-        initMediaSession()
+        if (!this::mediaSession.isInitialized) initMediaSession()
 
         val filter = IntentFilter().apply {
             priority = Integer.MAX_VALUE
@@ -565,12 +565,12 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     @TargetApi(Build.VERSION_CODES.O)
     private fun forceForeground() {
         val ctx = this at PlaybackService
+        NotificationHelper.createNotificationChannels(ctx.applicationContext)
         val stopped = playlistManager.player.playbackState == PlaybackStateCompat.STATE_STOPPED
         val notification = if (this::notification.isInitialized && !stopped) notification
         else NotificationHelper.createPlaybackNotification(ctx, false,
                 ctx.resources.getString(R.string.loading), "", "", null,
                 false, true, mediaSession.sessionToken, sessionPendingIntent)
-        NotificationHelper.createNotificationChannels(ctx.applicationContext)
         startForeground(3, notification)
         isForeground = true
         if (isVideoPlaying || Settings.showTvUi || stopped) hideNotification(true)
@@ -1017,12 +1017,12 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     fun load(mediaList: List<AbstractMediaWrapper>, position: Int) = playlistManager.load(mediaList, position)
 
     private fun updateMediaQueue() = launch {
+        if (!this at PlaybackService::mediaSession.isInitialized) initMediaSession()
         val ctx = this at PlaybackService
         val queue = withContext(Dispatchers.Default) {
             LinkedList<MediaSessionCompat.QueueItem>().also {
                 for ((position, media) in playlistManager.getMediaList().withIndex()) {
-                    var title: String? = media.nowPlaying
-                    if (title == null) title = media.title
+                    val title: String = media.nowPlaying ?: media.title
                     val builder = MediaDescriptionCompat.Builder()
                     builder.setTitle(title)
                             .setDescription(Util.getMediaDescription(MediaUtils.getMediaArtist(ctx, media), MediaUtils.getMediaAlbum(ctx, media)))
@@ -1033,7 +1033,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
                 }
             }
         }
-        mediaSession.setQueue(queue)
+        if (this at PlaybackService.lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) mediaSession.setQueue(queue)
     }
 
     @MainThread



More information about the Android mailing list