ExoPlayer:显示字幕

ExoPlayer: Show subtitles

我正在使用 ExoPlayer 播放来自 URL 的音频。我有 XML 格式的字幕,如下所示:

    [
    {
        "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
    }
]

我知道有一种方法可以在使用 ExoPlayer 播放时显示字幕。但是,我扫描了开发人员指南中的所有内容:ExoPlayer Developer's guide,但没有找到如何操作,因为文档真的很差。 另外,XML格式的字幕是否适合ExoPlayer,如果现在,应该如何转换成什么格式。 ExoPlayer忍者请帮助我)

您可以将 json 转换为 .str 格式并将其另存为文件。现在 ExoPlayer 可以显示 .str 文件。这是 str 格式的示例:

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. 

如果您的字幕 XML 来自 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);