[Android] Audioplayer: use correct scope for covers loading

Geoffrey Métais git at videolan.org
Wed Jul 3 13:37:47 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jul  3 13:37:06 2019 +0200| [6b66c40755253c2f4d63a3f1b72f639a8481726e] | committer: Geoffrey Métais

Audioplayer: use correct scope for covers loading

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

 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt  |  4 +-
 .../videolan/vlc/gui/view/AudioMediaSwitcher.kt    | 77 ++++++++++------------
 2 files changed, 38 insertions(+), 43 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 511a2260b..79f1c536d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -207,8 +207,8 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, CoroutineS
     private fun doUpdate() {
         if (activity === null || (isVisible && playlistModel.switchToVideo())) return
         binding.playlistPlayasaudioOff.visibility = if (playlistModel.videoTrackCount > 0) View.VISIBLE else View.GONE
-        binding.audioMediaSwitcher.updateMedia(playlistModel.service)
-        binding.coverMediaSwitcher.updateMedia(playlistModel.service)
+        binding.audioMediaSwitcher.updateMedia(this, playlistModel.service)
+        binding.coverMediaSwitcher.updateMedia(this, playlistModel.service)
 
         updatePlayPause()
         updateShuffleMode()
diff --git a/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt b/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
index 8e1546d1b..eedbfb9f1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
@@ -25,12 +25,9 @@ import android.graphics.Bitmap
 import android.net.Uri
 import android.util.AttributeSet
 import android.view.LayoutInflater
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.*
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.gui.helpers.AudioUtil
-import org.videolan.vlc.util.runIO
-import org.videolan.vlc.util.runOnMainThread
 
 
 @ObsoleteCoroutinesApi
@@ -87,47 +84,45 @@ abstract class AudioMediaSwitcher(context: Context, attrs: AttributeSet) : Fling
         setOnViewSwitchedListener(mViewSwitchListener)
     }
 
-    fun updateMedia(service: PlaybackService?) {
+    fun updateMedia(scope: CoroutineScope, service: PlaybackService?) {
         if (service == null) return
         val artMrl = service.coverArt
         val prevArtMrl = service.prevCoverArt
         val nextArtMrl = service.nextCoverArt
-        runIO(Runnable {
-            val coverCurrent = if (artMrl != null) AudioUtil.readCoverBitmap(Uri.decode(artMrl), 512) else null
-            val coverPrev = if (prevArtMrl != null) AudioUtil.readCoverBitmap(Uri.decode(prevArtMrl), 512) else null
-            val coverNext = if (nextArtMrl != null) AudioUtil.readCoverBitmap(Uri.decode(nextArtMrl), 512) else null
-            runOnMainThread(Runnable {
-                removeAllViews()
-
-                hasPrevious = false
-                previousPosition = 0
-
-                val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
-                if (service.hasPrevious()) {
-                    addMediaView(inflater,
-                            service.titlePrev,
-                            service.artistPrev,
-                            coverPrev)
-                    hasPrevious = true
-                }
-                if (service.hasMedia())
-                    addMediaView(inflater,
-                            service.title,
-                            service.artist,
-                            coverCurrent)
-                if (service.hasNext())
-                    addMediaView(inflater,
-                            service.titleNext,
-                            service.artistNext,
-                            coverNext)
-
-                if (service.hasPrevious() && service.hasMedia()) {
-                    previousPosition = 1
-                    scrollTo(1)
-                } else
-                    scrollTo(0)
-            })
-        })
+        scope.launch {
+            val coverCurrent = if (artMrl != null) withContext(Dispatchers.IO) { AudioUtil.readCoverBitmap(Uri.decode(artMrl), 512) } else null
+            val coverPrev = if (prevArtMrl != null) withContext(Dispatchers.IO) { AudioUtil.readCoverBitmap(Uri.decode(prevArtMrl), 512) } else null
+            val coverNext = if (nextArtMrl != null) withContext(Dispatchers.IO) { AudioUtil.readCoverBitmap(Uri.decode(nextArtMrl), 512) } else null
+            removeAllViews()
+
+            hasPrevious = false
+            previousPosition = 0
+
+            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
+            if (service.hasPrevious()) {
+                addMediaView(inflater,
+                        service.titlePrev,
+                        service.artistPrev,
+                        coverPrev)
+                hasPrevious = true
+            }
+            if (service.hasMedia())
+                addMediaView(inflater,
+                        service.title,
+                        service.artist,
+                        coverCurrent)
+            if (service.hasNext())
+                addMediaView(inflater,
+                        service.titleNext,
+                        service.artistNext,
+                        coverNext)
+
+            if (service.hasPrevious() && service.hasMedia()) {
+                previousPosition = 1
+                scrollTo(1)
+            } else
+                scrollTo(0)
+        }
     }
 
     protected abstract fun addMediaView(inflater: LayoutInflater, title: String?, artist: String?, cover: Bitmap?)



More information about the Android mailing list