Working with Subtitles in ActionScript

Subtitles can be manipulated and displayed in Flash for a video that has been encoded with subtitles, and they work just like subtitles in a movie. They allow for multiple language support in videos. Note: Before using the subtitled video file, a Flash SWF must be created that has/plays a USM video encoded with a subtitle text file. If you have followed the document in order, you should already have a fully encoded sample.usm video with two subtitles. If you haven’t, please refer to Getting Started: Adding Videos to Flash and the section on adding Subtitles to a video.

  1. Double click the mvplayer movie clip.

  2. Inside mvplayer, select the first keyframe of the actions layer on the timeline, and enter the code snippets below in the Actions panel, below any code that is already there.

  3. First, create a variable, subtitleChannelNumber, to hold the number of subtitle channels, and set it to 0: var subtitleChannelNumber = 0;

  4. Next, listen for the number of subtitle channels from the metadata of the video file. Note: Subtitle channel 0 turns subtitles off.

    ns.onMetaData = function(infoObject:Object)
    {
        ns.subtitleTrack = 1;
        subtitleChannelNumber = infoObject.subtitleTracksNumber;
        trace("Subtitle Channels: " +  subtitleChannelNumber);
    }
  5. Create a dynamic text field on the Stage, inside the mvplayer movie clip, and give it an instance name of ‘subtitle’. Be sure it is large enough, and that it has a noticeable color and font choice.

  6. The following callback function will be executed when the video hits a time in playback that has an embedded subtitle. The msg string will contain the string that was typed into the subtitle text file when the USM movie was encoded. In the case of the first subtitle, the string will be used as it was typed in the subtitle file. In the case of the second subtitle, which is the variable name subtitleId2, the variable name will be used to determine the message to be displayed via ActionScript. The contents of Msg will be displayed in the subtitle dynamic text field that was created in step 5.

    ns.onSubtitle = function(msg:String)
    {
        trace("Subtitle: " + msg);
        subtitle.text = msg;
    }
  7. Publish and test the Flash file in Scaleform Player by using the Scaleform Launcher panel or by dragging the SWF into an open Scaleform Player window. If everything is correct, the video’s subtitles should be seen as the video plays in both the output window as well as in the text field of the Flash file.