user3045705
user3045705

Reputation: 33

android kitkat videoView.addSubtitleSource

I'm trying to add closed captioning on a videoView and found a youtube clip that explained what to do.

In KitKat they added a new feature videoView.addSubtitleSource that adds a webvtt file that contains the subtitles. I could not get this to work.

Has anyone got this to work? Could you share a working example of how to do it?

Thanks in advance.

Upvotes: 2

Views: 4646

Answers (1)

lucky1928
lucky1928

Reputation: 8849

You should firstly enable "Close Caption" through: Menu->Settings->Accessibility->Captions, make sure to turn on it.

Then you need to add code to call the API to setup the WebVTT source looks like below:

private void showVideo(String path)
{
    Log.e(TAG, "showVideo");
    Uri uri = Uri.parse(path);
    mMc = new MediaController(this);

    mVideoView.setMediaController(mMc);
    mVideoView.setVideoURI(uri);
    mVideoView.addSubtitleSource(getSubtitleSource(path),
            MediaFormat.createSubtitleFormat("text/vtt",Locale.ENGLISH.getLanguage()));

    mVideoView.start();
}

private InputStream getSubtitleSource(String filepath) {
    InputStream ins = null;
    String ccFileName = filepath.substring(0,filepath.lastIndexOf('.')) + ".vtt";
    File file = new File(ccFileName);
    if (file.exists() == false)
    {
        Log.e(TAG,"no close caption file " + ccFileName);
        return null;
    }
    FileInputStream fins = null;
    try {
        fins = new FileInputStream(file);
    }catch (Exception e) {
        Log.e(TAG,"exception " + e);
    }
    ins = (InputStream)fins;
    return ins;
}

Then you should see your WebVTT Close Caption text displayed on your video.

Of course you can use MediaPlayer class to do it! Above code is using VideoView API for your reference!

Upvotes: 6

Related Questions