How to show subtitle in android ExoPlayer

Palakp picture Palakp · Jun 23, 2015 · Viewed 8k times · Source

I am using Exoplayer for streaming URL. I want to add srt file in exoplayer. But I think player not support srt file so I put my file content in 1 variable.

How to show subtitles in android exoplayer?

Answer

Faisal picture Faisal · Oct 26, 2015

A bit late but it might help others using Exoplayer 1.5.1 where DebugTrackRenderer has been removed.

Use,

DataSource textDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
    SingleSampleSource textSampleSource = new SingleSampleSource(subTitleUrl, textDataSource,
          MediaFormat.createTextFormat(MediaFormat.NO_VALUE,MimeTypes.APPLICATION_SUBRIP, MediaFormat.NO_VALUE, TrackRenderer.MATCH_LONGEST_US, null));
    TrackRenderer textRenderer = new TextTrackRenderer(textSampleSource, player,
          player.getMainHandler().getLooper());

Above code passes subTitleUrl to SingleSampleSource.

Then just enable your TextTrackRenderer that is enabled by default as,

player.setSelectedTrack(YourPlayer.TYPE_TEXT, YourPlayer.TRACK_DEFAULT);

It will do the trick.