Exoplayer: Subtitles Support in ExoPlayer 2.6.1

Created on 20 Feb 2018  路  5Comments  路  Source: google/ExoPlayer

Hello, I'm Trying to achieve a video .mp4 streaming with .srt subtitles.. i was following the example on #2017 but it seems that it no longer works.
here's what i used :
Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP, null, Format.NO_VALUE, Format.NO_VALUE, "en", null); MediaSource textMediaSource = new SingleSampleMediaSource(srtUri, mediaDataSourceFactory, textFormat, C.TIME_UNSET); MediaSource mediaSourceWithText = new MergingMediaSource(mediaSource, textMediaSource);

  • First problem is that Format.createTextSampleFormat returns an error message : Cannot resolve method 'createTextSampleFormat(null, java.lang.String, null, int, int, java.lang.String, null)

i tried using this method public static Format createTextSampleFormat(String id, String sampleMimeType, int selectionFlags, String language)
but no subtitles are showing in the video...

  • Second thing is SingleSampleMediaSource seems deprecated, it returns this message :

'SingleSampleMediaSource(android.net.Uri,
com.google.android.exoplayer2.upstream.DataSource.Factory, com.google.android.exoplayer2.Format, long)' is deprecated less... (Ctrl+F1)
This inspection reports where deprecated code is used in the specified inspection scope.

here's my .srt Uri : Uri srt = Uri.parse("http://download1509.mediafire.com/l807q3b4nqrg/q7m7ca7wbtakot8/Thor+3+.WEB+%28Colored%29.srt");
and here's the direct download link : http://download1509.mediafire.com/l807q3b4nqrg/q7m7ca7wbtakot8/Thor+3+.WEB+%28Colored%29.srt

so would u please show me anyway to achieve that ? thank you !

question

All 5 comments

To merge SRT subtitles with mediaSource please try:

Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
    null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
MediaSource textMediaSource = new SingleSampleMediaSource.Factory(mediaDataSourceFactory)
    .createMediaSource(srtUri, textFormat, C.TIME_UNSET);
mediaSource = new MergingMediaSource(mediaSource, textMediaSource);

It's important that srtUri points to the actual SRT file to play (not a web page, for example).

@andrewlewis Hello andrew! it works ! the subtiltes are appearing ! but there's a little problem :/

  • First one , my subs Language is Arabic.. so the characters are tottaly weird , they're something like Triangles and Squares and ??? stuff.. i think ExoPlayer doesn't support Arabic ?

  • Second one , the text seems to appear too small, is there anyway to scale it ?
    Thank you!

There's probably an issue with the encoding in the file. @AquilesCanta has kindly offered to take a look at that.

Even with the right encoding, RTL captions might not still render perfectly; see #2194.

Please have a look at SubtitleView for customizing the text size. If you're using SimpleExoPlayerView you can access this using getSubtitleView.

Hello @iToxicDeveloper, have you managed to successfully play the subtitles play with any player?

As @andrewlewis correctly points out, the encoding seems to be windows-1256 but ExoPlayer uses UTF-8 by default. Please try re-encoding your file (I managed to do it using iconv, on linux) and let us know if it works out correctly.

If you need to do this conversion client-side for your app, you might need to wrap the SubripDecoder to add support for the file's charset.

Thank you so much @AquilesCanta and @andrewlewis 馃憤

Was this page helpful?
0 / 5 - 0 ratings