2 Replies Latest reply on Jan 17, 2011 9:43 AM by yalp.com.br

    AS3 SRT Subtitle in SWF

    yalp.com.br

      Hi all,

       

      I've been looking at some tutorials in how to add SRT subtitles in flash. I found this grea article in base42 but i'm not finding a way to make it work can anyone give me a hand in how this works?

       

      PLS HELP!!!

       

      Let's say I have 4 files

       

      movie.fla (movie.swf)

      // I need to make the subtitle SRT show on the textBox_txt during my animation (timeline)

      import flash.external.ExternalInterface;

       

      var urlParser =  ExternalInterface.call ("nl.inlet42.data.subtitles.SubTitleData");
      var minhaLegenda:SubtitleParser = new SubtitleParser(urlParser);

       

      var urlData = ExternalInterface.call ("nl.inlet42.data.subtitles.SubTitleData");
      var carregaLegenda:SubTitleData = new SubTitleData(urlData);
      textBox_txt.text = subtitle.srt

       


      subtitle.srt

      1
      00:00:20,000 --> 00:00:24,400
      In connection with a dramatic increase
      in crime in certain neighbourhoods,

      2
      00:00:24,600 --> 00:00:27,800
      the government is implementing a new policy...

       

      SubtitleParser.as

      package nl.inlet42.data.subtitles {

           /**
            *     @author Jankees.van.Woezik
            */

           public class SubtitleParser {
                public static function parseSRT(data : String) : Array {
                     var result : Array = new Array();

                     var lines : Array;
                     var translation : SubTitleData;

                     var blocks : Array = data.split(/^[0-9]+$/gm);
                     for each (var block : String in blocks) {
                          translation = new SubTitleData();
                          lines = block.split('\n');
                          for each (var line : String in lines) {
                               //all lines in a translation block
                               if(trim(line) != "") {
                                    if(line.match("-->")) {
                                         //timecodes line
                                         var timecodes : Array = line.split(/[ ]+-->[ ]+/gm);
                                         if(timecodes.length != 2) {
                                              trace("Translation error, something wrong with the start or end time");                                  
                                         } else {

                                              translation.start = stringToSeconds(timecodes[0]);
                                              translation.end = stringToSeconds(timecodes[1]);
                                              translation.duration = translation.end - translation.start;

                                              if(translation.duration < 0) {
                                                   trace("Translation error, something wrong with the start or end time");                                  
                                              }
                                         }
                                    } else {
                                         //translation line
                                         if(translation.text.length != 0) line = "\n" + trim(line);
                                         translation.text += line;
                                    }
                               }
                          }
                          result.push(translation);
                     }         
                     return result;
                }

                public static function trim(p_string : String) : String {
                     if (p_string == null) {
                          return '';
                     }
                     return p_string.replace(/^\s+|\s+$/g, '');
                }

                /**
                 * Convert a string to seconds, with these formats supported:
                 * 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h / 00:01:53,800
                 *
                 * Special thanks to Thijs Broerse of Media Monks!
                 *
                 **/

                public static function stringToSeconds(string : String) : Number {
                     var arr : Array = string.split(':');
                     var sec : Number = 0;
                     if (string.substr(-1) == 's') {
                          sec = Number(string.substr(0, string.length - 1));
                     }else if (string.substr(-1) == 'm') {
                          sec = Number(string.substr(0, string.length - 1)) * 60;
                     }else if(string.substr(-1) == 'h') {
                          sec = Number(string.substr(0, string.length - 1)) * 3600;
                     }else if(arr.length > 1) {
                          if(arr[2] && String(arr[2]).indexOf(',') != -1) arr[2] = String(arr[2]).replace(/\,/, ".");

                          sec = Number(arr[arr.length - 1]);
                          sec += Number(arr[arr.length - 2]) * 60;
                          if(arr.length == 3) {
                               sec += Number(arr[arr.length - 3]) * 3600;
                          }
                     } else {
                          sec = Number(string);
                     }
                     return sec;
                }
           }
      }

       

      SubTitleData.as

      package nl.inlet42.data.subtitles {

           public class SubTitleData {
                public var text : String;
                public var start : Number;
                public var duration : Number;
                public var end : Number;

                public function SubTitleData(inText : String = "",inStart : Number = 0,inDuration : Number = 0,inEnd : Number = 0) {
                     text = inText;
                     start = inStart;
                     duration = inDuration;
                     end = inEnd;
                }

                public function toString() : void {
                     trace("nl.inlet42.data.subtitles.SubTitleData");
                }
           }
      }

       

      But every time I export my swf I keep getting the following errror message:


      SubtitleParser.as, Line 1    5001: The name of package 'nl.inlet42.data.subtitles' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. SubtitleParser.as

       

       

      Thanks a lot!!!

        • 1. Re: AS3 SRT Subtitle in SWF
          Jankeesvwoezik

          Hi,

           

          I came across your question by coincidence, i made those classes you mention.

           

          You made one small mistake, you have to make folder like the package name... (nl/inlet42/data/subtitles)

           

          If you need any help, drop me a line at jankees [at] base42 [dot] nl

           

          Good luck!

          • 2. Re: AS3 SRT Subtitle in SWF
            yalp.com.br Level 1

            Thanks for tour attention,

             

            I was looking at the code and I fixed the path but I'm still getting error messages

             

            /SubtitleParser.as, Line 8    1046: Type was not found or was not a compile-time constant: SubTitleData.

            /SubtitleParser.as, Line 12    1180: Call to a possibly undefined method SubTitleData.

             

            line 8        // var translation : SubTitleData;

            line 12     //translation = new SubTitleData();

             

            anyone has any ideias?

             

            thanks a lot