0
votes

I'm using ExoPlayer to play audio from a URL. I have subtitles in XML format that look like this:

    [
    {
        "id": 1,
        "startTime": "00:00:00",
        "endTime": "00:00:05",
        "textEn": "towns, in late 15th-century England.",
        "textRu": null
    },
    {
        "id": 2,
        "startTime": "00:00:05",
        "endTime": "00:00:10",
        "textEn": "the first thing to note is that in stark contrast to today, England was an overwhelmingly rural country.",
        "textRu": null
    }
]

I know there is a way to show subtitles while playing using ExoPlayer. However, I scanned everything in developer's guide: ExoPlayer Developer's guide , but did not find how to, because the documentation is really poor. Also, is the XML format of subtitles is suitable for ExoPlayer and if now, how should I convert them and to what format. ExoPlayer ninjas please help me)

2

2 Answers

1
votes

If your subtitle XML comes from HTTP:

Format subtitleFormat = Format.createTextSampleFormat(
                null, // An identifier for the track. May be null.
                MimeTypes.APPLICATION_SUBRIP, // The mime type. Must be set correctly.
                C.SELECTION_FLAG_DEFAULT, // Selection flags for the track.
                null); // The subtitle language. May be null.

MediaSource subtitleSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(Uri.parse(subtitleUrl), subtitleFormat, C.TIME_UNSET);

        MergingMediaSource mergedSource =
                new MergingMediaSource(videoSource, subtitleSource);
player.prepare(mergedSource);

0
votes

you can convert your json to .str format and save it as a file. now ExoPlayer can show .str file. here is a sample of str format:

1
00:02:17,440 --> 00:02:20,375
Senator, we're making
our final approach into Coruscant.

2
00:02:20,476 --> 00:02:22,501
Very good, Lieutenant.