[Android] Make media track accessors nullable

Nicolas Pomepuy git at videolan.org
Mon Oct 3 11:10:29 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Sep 27 13:40:13 2022 +0200| [5356af2614875df016b51beda49398784e7288ea] | committer: Duncan McNamara

Make media track accessors nullable

> https://code.videolan.org/videolan/vlc-android/commit/5356af2614875df016b51beda49398784e7288ea
---

 .../src/org/videolan/vlc/media/PlayerController.kt |  4 ++--
 .../vlc3/src/org/videolan/vlc/VersionDependant.kt  | 16 ++++++++++++----
 .../vlc4/src/org/videolan/vlc/VersionDependant.kt  | 22 +++++++++-------------
 3 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 5d45918f1..17cc40d8a 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -132,7 +132,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
 
     fun getAudioTracks(): Array<out VlcTrack>? = if (!mediaplayer.isReleased && mediaplayer.hasMedia()) mediaplayer.getAllAudioTracks() else emptyArray()
 
-    fun getAudioTrack():String = if (!mediaplayer.isReleased && mediaplayer.hasMedia()) mediaplayer.getSelectedAudioTrackId() else "-1"
+    fun getAudioTrack():String = if (!mediaplayer.isReleased && mediaplayer.hasMedia()) mediaplayer.getSelectedAudioTrack()?.getId() ?: "-1" else "-1"
 
     fun setVideoTrack(index: String) = !mediaplayer.isReleased && mediaplayer.hasMedia() && mediaplayer.setVideoTrack(index)
 
@@ -156,7 +156,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
 
     fun getSpuTracks(): Array<out VlcTrack>? = mediaplayer.getAllSpuTracks()
 
-    fun getSpuTrack() = mediaplayer.getSelectedSpuTrackId()
+    fun getSpuTrack() = mediaplayer.getSelectedSpuTrack()?.getId() ?: "-1"
 
     fun setSpuTrack(index: String) = mediaplayer.setSpuTrack(index)
 
diff --git a/application/vlc-android/vlc3/src/org/videolan/vlc/VersionDependant.kt b/application/vlc-android/vlc3/src/org/videolan/vlc/VersionDependant.kt
index e11c21a64..94d761ea5 100644
--- a/application/vlc-android/vlc3/src/org/videolan/vlc/VersionDependant.kt
+++ b/application/vlc-android/vlc3/src/org/videolan/vlc/VersionDependant.kt
@@ -48,12 +48,20 @@ fun IMedia.getAllTracks(): List<IMedia.Track> {
 
 fun MediaPlayer.getSelectedVideoTrack(): VlcTrack? = getCurrentVideoTrack()?.let { VlcTrackImpl(it) } ?: null
 
-fun MediaPlayer.getSelectedAudioTrackId(): String {
-    return getAudioTrack().toString()
+fun MediaPlayer.getSelectedAudioTrack(): VlcTrack? {
+    val currentTrackId = audioTrack
+   getAudioTracks().forEach {
+       if (it.id == currentTrackId) return VlcTrackImpl(it)
+   }
+    return null
 }
 
-fun MediaPlayer.getSelectedSpuTrackId(): String {
-    return spuTrack.toString()
+fun MediaPlayer.getSelectedSpuTrack(): VlcTrack? {
+    val currentTrackId = spuTrack
+    getSpuTracks().forEach {
+        if (it.id == currentTrackId) return VlcTrackImpl(it)
+    }
+    return null
 }
 
 fun MediaPlayer.getVideoTracksCount(): Int {
diff --git a/application/vlc-android/vlc4/src/org/videolan/vlc/VersionDependant.kt b/application/vlc-android/vlc4/src/org/videolan/vlc/VersionDependant.kt
index b077560cf..83d412180 100644
--- a/application/vlc-android/vlc4/src/org/videolan/vlc/VersionDependant.kt
+++ b/application/vlc-android/vlc4/src/org/videolan/vlc/VersionDependant.kt
@@ -32,32 +32,28 @@ fun IMedia.getAudioTracks():List<IMedia.AudioTrack> = getTracks(IMedia.Track.Typ
 
 fun IMedia.getAllTracks() = tracks.toList()
 
-fun MediaPlayer.getSelectedVideoTrack(): VlcTrack {
-    return VlcTrackImpl(getSelectedTrack(IMedia.Track.Type.Video))
+fun MediaPlayer.getSelectedVideoTrack(): VlcTrack? {
+    return getSelectedTrack(IMedia.Track.Type.Video)?.let { VlcTrackImpl(it) }
 }
 
-fun MediaPlayer.getSelectedAudioTrackId(): String {
-    return getSelectedTrack(IMedia.Track.Type.Audio).id
+fun MediaPlayer.getSelectedAudioTrack(): VlcTrack? {
+    return getSelectedTrack(IMedia.Track.Type.Audio)?.let { VlcTrackImpl(it) }
 }
 
-fun MediaPlayer.getSelectedSpuTrackId(): String {
-    return getSelectedTrack(IMedia.Track.Type.Text).id
-}
-
-fun MediaPlayer.getCurrentSpuTrack(): VlcTrack? {
-    return VlcTrackImpl(getSelectedTrack(IMedia.Track.Type.Text))
+fun MediaPlayer.getSelectedSpuTrack(): VlcTrack? {
+    return getSelectedTrack(IMedia.Track.Type.Text)?.let { VlcTrackImpl(it) }
 }
 
 fun MediaPlayer.getVideoTracksCount(): Int {
-    return getTracks(IMedia.Track.Type.Video).size
+    return getTracks(IMedia.Track.Type.Video)?.size ?: 0
 }
 
 fun MediaPlayer.getAudioTracksCount(): Int {
-    return getTracks(IMedia.Track.Type.Audio).size
+    return getTracks(IMedia.Track.Type.Audio)?.size ?: 0
 }
 
 fun MediaPlayer.getSpuTracksCount(): Int {
-    return getTracks(IMedia.Track.Type.Text).size
+    return getTracks(IMedia.Track.Type.Text)?.size ?: 0
 }
 
 fun MediaPlayer.setVideoTrack(index:String):Boolean {



More information about the Android mailing list