[Android] Fix NoSuchElementException

Nicolas Pomepuy git at videolan.org
Wed Jul 1 14:21:05 CEST 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jul  1 07:12:24 2020 +0200| [3f10dae599df15d4ddf14399ec079cccf825623c] | committer: Nicolas Pomepuy

Fix NoSuchElementException

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

 .../videolan/vlc/gui/dialogs/VideoTracksDialog.kt  | 29 ++++++++++------------
 .../vlc/gui/dialogs/adapters/TrackAdapter.kt       |  2 +-
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
index 8c4d3ee4a..d0ce76de2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/VideoTracksDialog.kt
@@ -72,42 +72,39 @@ class VideoTracksDialog : VLCBottomSheetDialogFragment() {
     }
 
     private fun onServiceChanged(service: PlaybackService?) {
-        service?.let { service ->
-            if (service.videoTracksCount <= 2) {
+        service?.let { playbackService ->
+            if (playbackService.videoTracksCount <= 2) {
                 binding.videoTracks.trackContainer.setGone()
                 binding.tracksSeparator3.setGone()
             }
-            if (service.audioTracksCount <= 0) {
+            if (playbackService.audioTracksCount <= 0) {
                 binding.audioTracks.trackContainer.setGone()
                 binding.tracksSeparator2.setGone()
             }
 
-            service.videoTracks?.let { trackList ->
-                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.first { it.id == service.videoTrack })
+            playbackService.videoTracks?.let { trackList ->
+                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.firstOrNull { it.id == playbackService.videoTrack })
                 trackAdapter.setOnTrackSelectedListener { track ->
-                    if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Setting track: $track")
-                    service.setVideoTrack(track.id)
+                    playbackService.setVideoTrack(track.id)
                 }
                 binding.videoTracks.trackList.adapter = trackAdapter
             }
-            service.audioTracks?.let { trackList ->
-                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.first { it.id == service.audioTrack })
+            playbackService.audioTracks?.let { trackList ->
+                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.firstOrNull { it.id == playbackService.audioTrack })
                 trackAdapter.setOnTrackSelectedListener { track ->
-                    if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Setting track: $track")
-                    service.setAudioTrack(track.id)
+                    playbackService.setAudioTrack(track.id)
                 }
                 binding.audioTracks.trackList.adapter = trackAdapter
             }
-            service.spuTracks?.let { trackList ->
-                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.first { it.id == service.spuTrack })
+            playbackService.spuTracks?.let { trackList ->
+                val trackAdapter = TrackAdapter(trackList as Array<MediaPlayer.TrackDescription>, trackList.firstOrNull { it.id == playbackService.spuTrack })
                 trackAdapter.setOnTrackSelectedListener { track ->
-                    if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Setting track: $track")
-                    service.setSpuTrack(track.id)
+                    playbackService.setSpuTrack(track.id)
                 }
                 binding.subtitleTracks.trackList.adapter = trackAdapter
                 if (trackList.isEmpty()) binding.subtitleTracks.emptyView.setVisible()
             }
-            if (service.spuTracks == null) binding.subtitleTracks.emptyView.setVisible()
+            if (playbackService.spuTracks == null) binding.subtitleTracks.emptyView.setVisible()
         }
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/adapters/TrackAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/adapters/TrackAdapter.kt
index 6becbf848..46277810a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/adapters/TrackAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/adapters/TrackAdapter.kt
@@ -30,7 +30,7 @@ import androidx.recyclerview.widget.RecyclerView
 import org.videolan.libvlc.MediaPlayer
 import org.videolan.vlc.databinding.VideoTrackItemBinding
 
-class TrackAdapter(private val tracks: Array<MediaPlayer.TrackDescription>, var selectedTrack: MediaPlayer.TrackDescription) : RecyclerView.Adapter<TrackAdapter.ViewHolder>() {
+class TrackAdapter(private val tracks: Array<MediaPlayer.TrackDescription>, var selectedTrack: MediaPlayer.TrackDescription?) : RecyclerView.Adapter<TrackAdapter.ViewHolder>() {
 
     lateinit var trackSelectedListener: (MediaPlayer.TrackDescription) -> Unit
 



More information about the Android mailing list