如何将字符串转换为 srt 文件

问题描述 投票:0回答:1

我有一个字符串,其中的时间格式正确,就像 SRT 文件一样。我还有用于显示视频的 ExpPlayer。我想在 ExoPlayer 中使用这个字符串作为 SRT 文件,怎么办?

例如这个字符串:

const val SRT_STRING = "1\n" +
    "00:01:52,042 --> 00:01:57,917\n" +
    "When the child was a child,\n" +
    "it walked with its arms swinging.\n" +
    "\n" +
    "2\n" +
    "00:01:58,125 --> 00:02:02,833\n" +
    "It wanted the stream to be a river,\n" +
    "the river a torrent...\n" +

这里应该用subtitleUri:

   val subtitle = SubtitleConfiguration.Builder(subtitleUri)

我考虑过将字符串写入文件并从该文件创建一个 Uri,但我不喜欢将所有文件保存在本地(当我将来添加更多文件时)。

有什么帮助吗?

kotlin exoplayer
1个回答
1
投票

您可以使用以下代码在ExoPlayer中使用字符串作为SRT文件:

import android.net.Uri

val SRT_STRING = "1\n" +
"00:01:52,042 --> 00:01:57,917\n" +
"When the child was a child,\n" +
"it walked with its arms swinging.\n" +
"\n" +
"2\n" +
"00:01:58,125 --> 00:02:02,833\n" +
"It wanted the stream to be a river,\n" +
"the river a torrent...\n" +
"\n" +
"3\n" +
"00:02:03,000 --> 00:02:07,917\n" +
"The torrent a sea,\n" +
"the sea an ocean...\n" +
"\n" +
"4\n" +
"00:02:08,125 --> 00:02:12,833\n" +
"And the ocean a world."

val subtitleUri = Uri.parse("data:text/srt;base64," + Base64.encodeToString(SRT_STRING.toByteArray(), Base64.DEFAULT))

val subtitle = SubtitleConfiguration.Builder(subtitleUri).build()

// Add the subtitle to the player.
player.addSubtitle(subtitle)

此代码将从 SRT 字符串创建一个 SubtitleConfiguration 对象并将其添加到播放器。然后播放器会根据SRT字符串中的时间信息显示字幕。

© www.soinside.com 2019 - 2024. All rights reserved.