Android Question ExoPlayer subtitles

MAGAREY

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

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2018-06-04_10.03.20.png



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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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))
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
SS-2018-06-04_10.03.20.png



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.
How to use 'createAudioSampleFormat' to add an audio resource .. what are the array parameters in this line :
B4X:
format.InitializeStatic("com.google.android.exoplayer2.Format").RunMethod("createTextSampleFormat", Array(Null, "application/x-subrip", 1, "en"))
PLEASE @Erel
 
Upvote 0
Top