5

How to set subtitles on ExoPlayer2? I have tried this tu bild MergingMediaSource:

    SingleSampleMediaSource singleSampleSource = new SingleSampleMediaSource(Uri.fromFile(new File("/sdcard/Download/a.vtt")), mediaDataSourceFactory,
        Format.createTextSampleFormat(null, MimeTypes.TEXT_VTT, null, Format.NO_VALUE, C.SELECTION_FLAG_DEFAULT, "se", null, 0),
        50000 //in us
         );
return new MergingMediaSource(new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
        mainHandler, eventLogger), singleSampleSource);

but I got this error:

 Unexpected exception loading stream
                                                         java.lang.NullPointerException: Attempt to get length of null array
                                                             at com.google.android.exoplayer2.source.SingleSampleMediaPeriod$SourceLoadable.load(SingleSampleMediaPeriod.java:272)
                                                             at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:295)
                                                             at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                             at java.lang.Thread.run(Thread.java:761)
Andrii Omelchenko
  • 12,030
  • 12
  • 40
  • 70
TheModularMind
  • 1,963
  • 1
  • 19
  • 36

2 Answers2

1
Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
                            null, Format.NO_VALUE, Format.NO_VALUE, "ar", null);

MediaSource subtitleSource = new SingleSampleMediaSource("link srt url",
                                    mediaDataSourceFactory, textFormat, C.TIME_UNSET);

MergingMediaSource mergedSource = new MergingMediaSource(mediaSource, subtitleSource);

more...

-2

That's because a bug in your Exoplayer version in class SourceLoadable, you can fix it changing this line:

private byte[] sampleData;

to

private byte[] sampleData = new byte[INITIAL_SAMPLE_SIZE];

Or using Exoplayer latest version.

Jovel Barrera
  • 11
  • 1
  • 5