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

Captura.PNG


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));




CapturaExoplayer.PNG
 
Upvote 0

mehdipass

Member
Hi, use this code to hide subtitles
B4X:
Sub SubtitleStyle(TextSize As Int,ForegroundColor As Int,BackgroundColor As Int,WindowColor As Int,ShadowColor As Int)
    Dim SubtitleView As JavaObject = SimpleExoPlayerView1
    SubtitleView = SubtitleView.RunMethod("getSubtitleView", Null)
    Dim TextSize2 As Float = TextSize
    SubtitleView.RunMethod("setFixedTextSize", Array(2, TextSize2))
    Dim CaptionStyleCompat As JavaObject
    If ShadowColor = 0 Then
        CaptionStyleCompat.InitializeNewInstance("com.google.android.exoplayer2.text.CaptionStyleCompat", Array ( _
           ForegroundColor, BackgroundColor, WindowColor, 0, 0,Typeface.SANS_SERIF))
    Else
        CaptionStyleCompat.InitializeNewInstance("com.google.android.exoplayer2.text.CaptionStyleCompat", Array ( _
    ForegroundColor, BackgroundColor, WindowColor, 2, ShadowColor,Typeface.SANS_SERIF)) 'for shadow
    End If
    SubtitleView.RunMethod("setStyle", Array(CaptionStyleCompat))
End Sub
Now
B4X:
SubtitleStyle(16,0,0,0,0)

If you want to change the video resolution, first get all the links related to the video resolution using the following code
B4X:
Sub Player_TrackChanged

    Dim jo As JavaObject = player1

    Dim getCurrentManifest As JavaObject  = jo.GetFieldJO("player").RunMethod("getCurrentManifest", Null)
    Dim hlsManifest As JavaObject = getCurrentManifest.GetFieldJO("masterPlaylist")
    'mediaPlaylistUrls is global list
    mediaPlaylistUrls = hlsManifest.GetField("mediaPlaylistUrls") ' return all url

End Sub
Now for example,
B4X:
Sub Button_Click
     player1.Prepare(player1.CreateHLSSource(mediaPlaylistUrls.Get(0)))
End Sub
To more information see this and HlsManifest
 
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
Top