Android Question Exoplayer - turn subtitles ON/OFF

blong

Active Member
Licensed User
Longtime User
Am using Exoplayer 3.01 and can set select subtitles fine.... with code like this

My subroutine:
Private Sub SetPreferredTextLanguage(Language As String)

    Dim jo As JavaObject = player_exo

    Dim TrackSelector As JavaObject = jo.GetField("trackSelector")

    TrackSelector.RunMethod("setParameters", Array( _

        TrackSelector.RunMethodJO("buildUponParameters", Null) _

        .RunMethod("setPreferredTextLanguage", Array(Language))))

End Sub

But if my file has say two TEXT languages "en" and "th" ... will select OK.

I tried passing "" to above but it just went to 1st track by default

Also set their style .... colour, size, background etc....

Thanks in advance
 
Last edited:

blong

Active Member
Licensed User
Longtime User
Found a solution to Disable
My subroutine:
Private Sub DisableSubtitles()
    Dim jo As JavaObject = player_exo
    Log("enter disable")
    Dim trackSelector As JavaObject = jo.GetField("trackSelector")
    Dim parametersBuilder As JavaObject = trackSelector.RunMethodJO("buildUponParameters", Null)

    ' Access the track selector internals
    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))
            Dim mime As String = format.GetField("sampleMimeType")
            If mime.contains("x-subrip") Then
                ' Disable this text renderer
                Log("subs disabled")
                parametersBuilder.RunMethodJO("setRendererDisabled", Array(i, True))
            End If
        Next
    Next

    ' Apply the new parameters to the track selector
    trackSelector.RunMethod("setParameters", Array(parametersBuilder.RunMethod("build", Null)))
End Sub

and if you change the
code line:
parametersBuilder.RunMethodJO("setRendererDisabled", Array(i, True))

to Array(i,False)

then the routine will enable subtitles.
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Still have problem with this code to change subtitle colours
my sub:
Sub SubtitleStyle(TextSize As Int, ForegroundColor As Int, BackgroundColor As Int, WindowColor As Int, ShadowColor As Int)
    Try
        Dim SubtitleView As JavaObject = ExoPlayerView
        SubtitleView = SubtitleView.RunMethod("getSubtitleView", Null)
        If SubtitleView <> Null And SubtitleView.IsInitialized Then
            Dim TextSize2 As Float = TextSize
            SubtitleView.RunMethod("setFixedTextSize", Array(2, TextSize2))

            Dim CaptionStyleCompat As JavaObject
            If ShadowColor = 0 Then
                CaptionStyleCompat.InitializeNewInstance("androidx.media3.exoplayer.text.CaptionStyleCompat", Array( _
                   ForegroundColor, BackgroundColor, WindowColor, 0, 0, Typeface.DEFAULT))
            Else
                CaptionStyleCompat.InitializeNewInstance("androidx.media3.exoplayer.text.CaptionStyleCompat", Array( _
                    ForegroundColor, BackgroundColor, WindowColor, 2, ShadowColor, Typeface.DEFAULT)) 'for shadow
            End If
            SubtitleView.RunMethod("setStyle", Array(CaptionStyleCompat))
            Log("Subtitle customization applied")
        Else
            Log("SubtitleView not found")
        End If
    Catch
        Log(LastException.Message)
    End Try
End Sub


I get an error with the androidx.media3.exoplayer.text statement saying
java.lang.ClassNotFoundException: androidx.media3$exoplayer$text$CaptionStyleCompat

any ideas please ???
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Finally solved all of my queries.... I attach the complete example that allows selecting a subtitle track, changing font style etc and enabling/disabling subtitles

Final test example:
Sub Process_Globals
    'These global variables will be declared once when the application starts.   
    'These variables can be accessed from all modules.
    Dim player_exo As SimpleExoPlayer
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ExoPlayerView As SimpleExoPlayerView
    Dim btnClose As Button
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If(FirstTime) Then
        
        player_exo.Initialize("Player")
        '
        '  change next line to point to your movie file
        '
        player_exo.Prepare(player_exo.CreateFileSource("/storage/0403-0201/Android/data/baz.movie.player/files","ShogSubs.mkv"))
        
    End If
    
    Activity.LoadLayout("EXOmain")
    ExoPlayerView.Player=player_exo
        
    
End Sub
Sub Player_TrackChanged
    
End Sub
Sub Player_Ready
    Log("Player is ready")
    Dim jo As JavaObject = player_exo
    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("=======Sample MIME Type: " & Format.GetField("sampleMimeType") & " =========")
            '
            '    following lines show what information is available
            '
            Log("Codecs: " & Format.GetField("codecs"))
            Log("Bitrate: " & Format.GetField("bitrate"))
            Log("Width: " & Format.GetField("width"))
            Log("Height: " & Format.GetField("height"))
            Log("Frame Rate: " & Format.GetField("frameRate"))
            Log("Channel Count: " & Format.GetField("channelCount"))
            Log("Sample Rate: " & Format.GetField("sampleRate"))
            Log("Selection Flags: " & Format.GetField("selectionFlags"))
            Log("Language: " & Format.GetField("language"))
            Log("Label: " & Format.GetField("label"))
            Log("Role Flags: " & Format.GetField("roleFlags"))
            Log("Average Bitrate: " & Format.GetField("averageBitrate"))
            Log("Peak Bitrate: " & Format.GetField("peakBitrate"))
        Next
    Next
    
    SetPreferredAudioLanguage("en")
    
    Subtitles_OFF(False)    '  set to true to get preferred subtitle language
    
    SetPreferredTextLanguage("th")      ' th is for Thai language
    '
    ' set colours as required
    '
    Dim ForegroundColor As Int = 0xFFFFFF00 ' Yellow text
    Dim BackgroundColor As Int = 0x00000000 ' Black background with high transparency
    Dim WindowColor As Int = 0x30000000 ' Semi-transparent window

    SubtitleStyle(12,2,ForegroundColor,BackgroundColor,WindowColor)

    
End Sub

Sub SubtitleStyle(TextSize As Int, Fraction As Int, ForegroundColor As Int, BackgroundColor As Int, WindowColor As Int)
    Try
        Dim SubtitleView As JavaObject = ExoPlayerView
        SubtitleView = SubtitleView.RunMethod("getSubtitleView", Null)
        If SubtitleView <> Null And SubtitleView.IsInitialized Then
            Dim TextSize2 As Float = TextSize
            SubtitleView.RunMethod("setFixedTextSize", Array(2, TextSize2))
            
            Dim fraction2 As Float = 0.01 * Fraction 'input as percentage  0.0 = bottom   0.5 halfway up
            SubtitleView.RunMethod("setBottomPaddingFraction", Array(fraction2))

            Dim CaptionStyleCompat As JavaObject
            CaptionStyleCompat.InitializeNewInstance("androidx.media3.ui.CaptionStyleCompat", Array (ForegroundColor, BackgroundColor, WindowColor, 0,0, Typeface.MONOSPACE))
            SubtitleView.RunMethod("setStyle", Array(CaptionStyleCompat))

            Log("Subtitle customization applied")
        Else
            Log("SubtitleView not found")
        End If
    Catch
        Log(LastException.Message)
    End Try
End Sub

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

Private Sub Subtitles_OFF(myvalue As Boolean)
    Dim jo As JavaObject = player_exo
    Log("enter disable")
    Dim trackSelector As JavaObject = jo.GetField("trackSelector")
    Dim parametersBuilder As JavaObject = trackSelector.RunMethodJO("buildUponParameters", Null)

    ' Access the track selector internals
    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))
            Dim mime As String = format.GetField("sampleMimeType")
            If mime.contains("x-subrip") Then
                ' Disable or enable this text renderer
                parametersBuilder.RunMethodJO("setRendererDisabled", Array(i, myvalue))
            End If
        Next
    Next
    ' Apply the new parameters to the track selector
    trackSelector.RunMethod("setParameters", Array(parametersBuilder.RunMethod("build", Null)))
End Sub

Sub SelectSubtitleTrack(preferredSubtitleLanguage As String)
    Dim jo As JavaObject = player_exo
    Dim trackSelector As JavaObject = jo.GetField("trackSelector")
    Dim TrackGroups As JavaObject = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
    Dim builder As JavaObject = trackSelector.RunMethodJO("buildUponParameters", 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))
            Dim mime As String = Format.GetField("sampleMimeType")
            Dim language As String = Format.GetField("language")
            Log("Track " & i & "-" & j & " MIME type: " & mime & ", Language: " & language)
            If mime.Contains("x-subrip") And language = preferredSubtitleLanguage Then
                Log("Found preferred subtitle track at " & i & "-" & j)
                
                ' Initialize SelectionOverride with correct parameters
                Dim indices() As Int = Array As Int(j)
                Dim selectionOverride As JavaObject
                selectionOverride.InitializeNewInstance("androidx.media3.exoplayer.trackselection.DefaultTrackSelector$SelectionOverride", Array(i, indices))

                ' Apply the selection override using the parameters builder
                Dim trackSelections As JavaObject = builder.RunMethodJO("setRendererDisabled", Array(2, False)) ' Enable text renderer
                trackSelections.RunMethod("setSelectionOverride", Array(i, TrackGroup, selectionOverride))
                
                trackSelector.RunMethod("setParameters", Array(builder.RunMethod("build", Null)))
                
                Log("Selected subtitle track: " & language)
                Return
            End If
        Next
    Next
    Log("No preferred subtitle track found.")
End Sub

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



Private Sub btnClose_Click
    
    Activity.Finish
    ExitApplication
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Hope this may help others...

NOTE : It uses the latest version of Exoplayer 3.01

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