Android Core

Android – Google TV Subtitle Support

One of the more frequent questions from users of Serenity for Google TV, is the ability to play back subtitles with the movie. Plex does provide the information if it is there, but until Android 4.1 (aka Jelly Bean), there was no native subtitle support for the video player. Even then it seems to have problems with reading streams for the subtitles. Google TV devices are currently based on Android 3.2 (Honeycomb) which doesn’t have an api to play subtitles back. Plex in these cases would normally Transcode the subtitles into the video stream, however, Serenity doesn’t support transcoding. So what to do. Simple turn to open source software for some help.

Get it on Google Play

Hidden on GitHub is subtitleConverter by J. David Requejo. This was a PHD research project, and allows for converting from one subtitle format to another. The nice thing is that the TimedTextObject contains a general format for subtitles. Once it is in this format it is a matter of navigating the tree and checking the time offsets for when the title is displayed and when it should disappear.

Since Google TV devices don’t have native support for displaying the subtitles, how do we go about this. Serenity uses a modified version of the MediaController, and SurfaceView. The main activity contains the following layout xml:

<?xml version='1.0' encoding='utf-8'?>
<RelativeLayout xmlns:android=''
    android:keepScreenOn='true' >
<SurfaceView android:id='@+id/surfaceView'

<TextView android:id='@+id/txtSubtitles'


This contains the surfaceview for the video to be drawn on, and a TextView to be used to display the subtitles. For knowing when to display the subtitles during playback we use a Handler with a small time delay for when it should execute. The first time it is called with it’s runnable, it executes immediately. Every subsequent execution is delayed by 100 milliseconds.

private Handler subtitleDisplayHandler = new Handler();
private Runnable subtitle = new Runnable() {
 public void run() {
   if (isMediaPlayerStateValid() && mediaPlayer.isPlaying()) {
      int currentPos = mediaPlayer.getCurrentPosition();
      Collection<Caption> subtitles =  srt.captions.values();
      for(Caption caption : subtitles) {
         if (currentPos >= caption.start.getMilliseconds() && currentPos <= caption.end.getMilliseconds()) {
  	 } else if (currentPos > caption.end.getMilliseconds()) {
   subtitleDisplayHandler.postDelayed(this, SUBTITLE_DISPLAY_CHECK);

The postDelayed will re-execute the Handler with the runnable by the amount of milliseconds specified. The onTimedText is what is responsible for displaying or hiding the subtitles during playback.

public void onTimedText(Caption text) {
   TextView subtitles = (TextView) findViewById(;
   if (text == null) {

Since the subtitles can be marked up with HTML to specify italics, bold, or underline we use the Html.fromHtml method to set the text as appropriate and set the visibility. If text came in null, the view is hidden. The whole process is kicked off by a Async task that retrieves the Subtitles from Plex as a stream.

public class SubtitleAsyncTask extends AsyncTask<Void, Void, Void> {
  protected Void doInBackground(Void... params) {
    if (subtitleURL != null) {
       try {
          URL url = new URL(subtitleURL);
          InputStream stream = url.openStream();
          FormatSRT formatSRT = new FormatSRT();
          srt = formatSRT.parseFile(stream);
       } catch (Exception e) {
          Log.e(getClass().getName(), e.getMessage(), e);
    // TODO Auto-generated method stub
    return null;

This just reads the stream, and parses the SubRip (SRT) formatted subtitles files into a TimedTextObject, and then starts the handler.

It is important to make sure when your Activity finishes that you remove any callbacks on the Handler. So make sure to do the following in your onFinish method:


This should work regardless of Android version. It has been tested on Android 3.2 through the lastest JellyBean release. If you need to manipulate Subtitles, use them, or covert them to other formats, you may want to try out the subtitleConverter project. Expect to see this support with Serenity for Google TV v1.1.0.

Reference: Android – Google TV Subtitle Support from our JCG partner David Carver at the Intellectual Cramps blog.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button