2

I have a video player app which used the Exoplayer library to play videos but I want to be able to download and add subtitles dynamically using something like OpenSubtitles and then add them to the currently playing video.

Video players with similar features are MX Player and X Player.

How can I be able to achieve a similar functionality. Your answers are highly appreciated!

2 Answers2

1

Yes, actually. OpenSubtitles.org has a nice API for searching and downloading.

Here you go https://opensubtitles.stoplight.io/docs/opensubtitles-api/e3750fd63a100-getting-started

Usman Adam
  • 11
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '22 at 11:49
0

You can download subtitles separately or checking downloading media and add them using sideloading subtitles or you can use SingleSampleMediaSource like the following:

    MediaSource[] mediaSources = new MediaSource[subtitlesCount + videoTrack];  
    mediaSources[0] = new ProgressiveMediaSource.Factory(new FileDataSource.Factory())
                        .createMediaSource(MediaItem.fromUri(videoPath));
//add subtitles tracks, and use correct format
    SingleSampleMediaSource.Factory singleSampleSourceFactory = new SingleSampleMediaSource.Factory(new DefaultDataSourceFactory(context));
    for (int i = 0; i < videoInfo.getSubtitlePaths().size(); i++) {
        mediaSources[i + videoTrack] = singleSampleSourceFactory.createMediaSource(
                    new MediaItem.Subtitle(Uri.parse(subtitlePath), MimeTypes.TEXT_VTT, language), C.TIME_UNSET);
    }
//use MergingMediaSource to combine the media sources
    MergingMediaSource mergedSource = new MergingMediaSource(mediaSources);
....init player
mPlayer.addMediaSource(mergedSource);
mPlayer.prepare();
Nermeen
  • 15,883
  • 5
  • 59
  • 72