Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert a string to srt file

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading