I got a solution. I convert srt file to 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 the playerβs activity, the subtitles are transferred to the respected builder, in this I send 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 the HlsRendererBuilder class, pass it to TrackRenderer, // Create a debug rendering.
TrackRenderer debugRenderer = debugTextView != null ? new DebugTrackRenderer(debugTextView, player, videoRenderer,context,subttl) : null;
And in the DebugTrackRenderer class in the startup method, use the code below
public void run() { String data1 = subttl.getText(getCurrentPositionUs()); player.onText(String.valueOf(data1)); }