Android Question ExoPlayer subtitles

MAGAREY

Member
Licensed User
its posible to load srt in exoplayer, i think in the lib is missing that part.
any advice?
thanks in advance
 

Erel

Administrator
Staff member
Licensed User



1. Download the subtitles and save them as UTF8 file:
B4X:
    Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("http://45.79.158.247/test.srt")
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim s As String = j.GetString2("windows-1252")
       File.WriteString(File.DirInternal, "1.srt", s)
   End If
   j.Release
2. Created a merged source with the video source and subtitles source:
B4X:
Dim subtitles As Object = CreateSingleSampleMediaSource("file://" & File.Combine(File.DirInternal, "1.srt"))
Dim video As Object = player1.CreateUriSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
sources.Add(CreateMergedSource(Array(video, subtitles)))
Subs:
B4X:
Sub CreateMergedSource(Sources() As Object) As Object
   Dim arr As JavaObject
   arr.InitializeArray("com.google.android.exoplayer2.source.MediaSource", Sources)
   Dim m As JavaObject
   Return m.InitializeNewInstance("com.google.android.exoplayer2.source.MergingMediaSource", Array(arr))
End Sub

Sub CreateSingleSampleMediaSource (Uri As String) As Object
   Dim format As JavaObject
   format = format.InitializeStatic("com.google.android.exoplayer2.Format").RunMethod("createTextSampleFormat", Array(Null, "application/x-subrip", 1, "en"))
   Dim source As JavaObject
   Dim u As Uri
   u.Parse(Uri)
   Dim exo As JavaObject = player1
   source.InitializeNewInstance("com.google.android.exoplayer2.source.SingleSampleMediaSource", Array(u, exo.RunMethod("createDefaultDataFactory", Null), _
       format, -9223372036854775807))
   Return source
End Sub
Depends on ContentResolver library.
 

Erel

Administrator
Staff member
Licensed User
Changing the subtitles style and text size:
B4X:
Dim SubtitleView As JavaObject = SimpleExoPlayerView1
SubtitleView = SubtitleView.RunMethod("getSubtitleView", Null)
Dim TextSize As Float = 20
SubtitleView.RunMethod("setFixedTextSize", Array(2, TextSize))
Dim CaptionStyleCompat As JavaObject
Dim ForegroundColor As Int = Colors.Red
Dim BackgroundColor As Int = Colors.Yellow
Dim WindowColor As Int = Colors.White
CaptionStyleCompat.InitializeNewInstance("com.google.android.exoplayer2.text.CaptionStyleCompat", Array ( _
   ForegroundColor, BackgroundColor, WindowColor, 0, 0, Typeface.MONOSPACE))
SubtitleView.RunMethod("setStyle", Array(CaptionStyleCompat))
 

Sasuke Sama

Active Member
Licensed User
hi i know this old but can somebody tell me what the sources is?
when i copy the code above im getting error with this line
B4X:
sources.Add(CreateMergedSource(Array(video, subtitles)))
undecleard var "sources"
 

DonManfred

Expert
Licensed User
Top