Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show subtitle in android ExoPlayer

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?

like image 648
Palakp Avatar asked Jun 23 '15 05:06

Palakp


2 Answers

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.

like image 69
Faisal Avatar answered Oct 16 '22 16:10

Faisal


I got the solution. I convert srt file into TTML file using TimedTextFileFormat Below is my code to convert srt to TTML,

private Subtitle subttl;

private void parseSubtitile(String subtitle) {

// subtitle is the srt file content
            // TODO Auto-generated method stub
            // StringBuilder buf = new StringBuilder();

InputStream json = null;
json = null;
        json = new ByteArrayInputStream(subtitle.getBytes());
    // FormatTTML formate = new FormatTTML();
    InputStream is = null;
    try {
        // TimedTextObject ttmlObj=formate.parseFile("Testing", json);
        TimedTextFileFormat ttff = new FormatSRT();
        TimedTextObject tto = ttff.parseFile("Test", json);
        // IOClass.writeFileTxt("test1", tto.toTTML());

        String data = tto.toTTML();
        is = new ByteArrayInputStream(data.getBytes());
        Log.d("web", data.toString());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (FatalParsingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    TtmlParser parser = new TtmlParser();
    try {
        subttl = parser.parse(is, null, 0);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

After that in player activity pass subtitle to respected builder, In this i am sending to HlsRendererBuilder.

private RendererBuilder getRendererBuilder() {
        String userAgent = Util.getUserAgent(this, "ExoPlayerDemo");
        switch (contentType) {

    case DemoUtil.TYPE_HLS:
        return new HlsRendererBuilder(this, userAgent,
                contentUri.toString(), debugTextView, audioCapabilities,
                    subttl);
}

From HlsRendererBuilder class pass it to TrackRenderer, // Build the debug renderer.

TrackRenderer debugRenderer = debugTextView != null
    ? new DebugTrackRenderer(debugTextView, player, videoRenderer,context,subttl) : null;

And in DebugTrackRenderer class in run method use below code,

public void run() {
    String data1 = subttl.getText(getCurrentPositionUs());

    player.onText(String.valueOf(data1));


} 
like image 28
Palakp Avatar answered Oct 16 '22 14:10

Palakp