Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ExoPlayer2 subtitles

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)
like image 705
TheModularMind Avatar asked Nov 09 '22 08:11

TheModularMind


1 Answers

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...

like image 148
AbdulMomen عبدالمؤمن Avatar answered Nov 15 '22 04:11

AbdulMomen عبدالمؤمن