I have a string, in which the timing is formatted correctly like a SRT file. I also have ExpPlayer for displaying videos. I want to use this string in ExoPlayer as SRT file, how to do that?
For example this string:
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" +
Should be used here as subtitleUri:
val subtitle = SubtitleConfiguration.Builder(subtitleUri)
I thought about writing the string inside a file and create a Uri from the file, but I don’t like to save all the files locally(when I add more in the future).
Any help?
>Solution :
you can use the following code to use a string as an SRT file in ExoPlayer:
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)
This code will create a SubtitleConfiguration object from the SRT string and add it to the player. The player will then display the subtitles according to the timing information in the SRT string.