How to show subtitles in android ExoPlayer

I am using Exoplayer for a streaming URL. I want to add srt file to exoplayer. But I think that the player does not support the srt file, so I put the contents of my file in 1 variable.

How to show subtitles in exoplayer for Android?

+9
source share
3 answers

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)); } 
+2
source

A bit late, but it can help other users use Exoplayer 1.5.1, where DebugTrackRenderer is 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, which is enabled by default, as

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

It will be a trick.

+5
source

Subtitles in ExoPlayer Work in progress .

As indicated on version 1.4.0 :

(WorkInProgress) - First steps to support stylized + positioned subtitles.

0
source

Source: https://habr.com/ru/post/989571/


All Articles