Android Question Select and change Exoplayer audio tracks or subtitles

nedium

Active Member
Licensed User
Longtime User
Hello everybody

first of all thanks for any information or help you can provide

I wanted to know without the new version of Exoplayer 2.13 available for B4A, could you help me how to create a code for the selection of subtitles or change of audio track that comes in the same video

using this code

B4X:
Sub Globals
    Dim VIDEOS_TRACK_GROUP_INDEX As Int
    Dim TrackGroups As JavaObject
End Sub

Sub Player_TrackChanged

    Dim jo As JavaObject = player1
    TrackGroups = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
    For i = 0 To TrackGroups.GetField("length") - 1
        Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
        For j = 0 To TrackGroup.GetField("length") - 1
            Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
            Log(Format) 'ignore
            Dim mime As String = Format.GetField("sampleMimeType")
            If mime.StartsWith("video") Then
                If VIDEOS_TRACK_GROUP_INDEX = 0 Then VIDEOS_TRACK_GROUP_INDEX = i
            End If
        Next
    Next

End Sub

I manage to see that this video has



I would like to be able to select a subtitle or an audio track change, both to add and to remove them

I send an example of what I want



regards
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Player_Ready
    Log("Ready")
    Dim jo As JavaObject = player1
    Dim TrackGroups As JavaObject = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
    For i = 0 To TrackGroups.GetField("length") - 1
        Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
        For j = 0 To TrackGroup.GetField("length") - 1
            Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
            Log(Format.GetField("language"))
            
        Next
    Next
    SetPreferredAudioLanguage("en")
End Sub

Private Sub SetPreferredAudioLanguage(Language As String)
    Dim jo As JavaObject = player1
    Dim TrackSelector As JavaObject = jo.GetField("trackSelector")
    TrackSelector.RunMethod("setParameters", Array( _
        TrackSelector.RunMethodJO("buildUponParameters", Null) _
        .RunMethod("setPreferredAudioLanguage", Array(Language))))
End Sub
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Changing
setPreferredAudioLanguage
For
setPreferredTextLanguage

Subtitles can be displayed, selecting by the type of language.
And if that code is executed with the variable NULL, the subtitle is currently hidden.


B4X:
Private Sub SetPreferredTextLanguage(Language As String)
    Dim jo As JavaObject = player1
    Dim TrackSelector As JavaObject = jo.GetField("trackSelector")
    TrackSelector.RunMethod("setParameters", Array( _
        TrackSelector.RunMethodJO("buildUponParameters", Null) _
        .RunMethod("setPreferredTextLanguage", Array(Language))))
End Sub



But there is a small problem with this video is that it has 2 subtitles of the same language, you only select the first one, you cannot choose the second.

In the getCurrentTrackGroups list, order the tracks by an ID, it would be a good option to be able to select the tracks that the video contains by the ID, since it is the only value among all of them that the video contains.


Greetings and thanks
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
I have found this information to select a specific track.

I can't use it with the JavaObject library.

Java:
 SelectionOverride selectionOverride = new SelectionOverride(groupIndex, trackIndices);
 trackSelector.setParameters(
     trackSelector
         .buildUponParameters()
         .setSelectionOverride(rendererIndex, rendererTrackGroups, selectionOverride));




 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
Thanks for the help @mehdipass, I will keep it in mind.

I wanted to know if you have any code, where I can select the audio track or subtitle by the ID number of the track.

I can select by language but if there are 2 with the same language, just select the first option.

Thank
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…