Search code examples
androidandroid-videoviewexoplayerexoplayer2.xandroid-video-player

com.google.android.exoplayer2.text.SubtitleDecoderException


I'm creating subtitles by simply writing a text file and saving it with .srt extension. And all subtitle files which i have created is working fine. (I just checked it with MxPlayer)

Now I'm building a video player which needs to display the subtitles which I have created.

Initially I tried to load subtitles from web just to prepare the code base. And it worked

val uri = Uri.parse("https://bitdash-a.akamaihd.net/content/sintel/hls/subtitles_en.vtt")
val subtitleFormat: Format = Format.createTextSampleFormat(
            null,  // An identifier for the track. May be null.
            MimeTypes.TEXT_VTT,  // The mime type. Must be set correctly.
            Format.NO_VALUE,  // Selection flags for the track.
            "en"
        )
var mediaDataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, null))
var subtitleSource = SingleSampleMediaSource(uri, mediaDataSourceFactory,
            subtitleFormat,
            C.TIME_UNSET)
...
...

var videoSource: MediaSource = buildMediaSource(video_uri!!)
var mediaSource: MediaSource =  MergingMediaSource(videoSource, subtitleSource)

Now all I have to do is load subtitle file from the local folder which is located at

/storage/emulated/0/Android/data/com.company.appname/files/Subtitle/shortmovie.2020.720p.x264.mp4.en.srt

I know web file is different and local file is different. But don't know whether it is causing problem or not*

To load I'm following below snippet

fun getSubtitleFilePath(context: Context, subtitleFileName: String?): File {
        val path = context.getExternalFilesDir(null)
        return File(path, "$appFolderName/${subtitleFileName}.srt")
}

val subtitleFilePath = getSubtitleFilePath(this, "shortmovie.2020.720p.x264.mp4.en")
val uri = Uri.parse(subtitleFilePath.toURI().toString())

That's all, I just replaced uri with local subtitle file.

Main Problem

when I try load the subtitles the player is not playing the video. And I'm getting an error

Playback error.
com.google.android.exoplayer2.ExoPlaybackException: com.google.android.exoplayer2.text.SubtitleDecoderException: com.google.android.exoplayer2.ParserException: Expected WEBVTT. Got 1

Caused by:
com.google.android.exoplayer2.text.SubtitleDecoderException: com.google.android.exoplayer2.ParserException: Expected WEBVTT. Got 1

I have no idea what this error about. I just went through Github issue and found no solution for this.

How to solve this issue?


Solution

  • Since, no one answered my question I found answer myself.

    • first thing is .vtt and .srt are slightly different.
      in .vtt the first line is a text WEBVTT check this
      in .srt there is no such thing

    • secondly, very important thing is MimeTypes in subtitleFormat it should be MimeTypes.APPLICATION_SUBRIP for .srt and MimeTypes.TEXT_VTT for .vtt

    • If you rename .srt to .vtt and added that first line WEBVTT manually some parsing exception will come. So, better check webvtt file whether it is proper or not.

    That's all now things are working fine.