highest safe dose of levitralevitra takecompare levitra cialis viagrageneric levitra onlinecheap herbal vardenafillevitra online uklevitra increases sperm countwomen\s vardenafillevitra samplelevitra slide kitmail order levitralevitra prescription onlinesoft levitraPurchase Levitra pillthe buy levitrageneric levitra canadaproduct team levitravardenafil 90 pills Buylevitra sale 32free sample prescription for levitralevitra trial offermedication levitranon prescription vardenafillevitra online shop in uklevitra medicareinformation levitralevitra on line saleslevitra japanqry vardenafilSale vardenafil 20 pillscheap herbal levitraidentify vardenafillevitra alternative lavitraflomax levitraGeneric Levitra 20 mg Salelevitra couponus vardenafilbuy levitra 10mglevitra commercial modelsforum levitraOnline vardenafil Courier shipping10 mg vs 20 mg levitralevitra dosage directionstoo much levitrawhere to puchase vardenafil onlinecheap levitra on linecialis levitra vardenafilhow levitra workqueen levitravardenafil 180 pills orderlevitra beogradlevitra birth control pillslevitra orallow cost alternatives and vardenafilLevitra 120 pills BuyBuy vardenafil Free Airmail shippingordering levitra onlinecialis levitra viagra comparebuy vardenafil online australiaorder levitra cheap pricevardenafil saleanyone tried generic levitravardenafil softabsbuy levitra viagra onlinelevitra user reviewneed for increasing amounts of levitralevitra online purchasewhat is vardenafilvardenafil rrp australia costvardenafil fast shippingvardenafil no prescriptionlevitra online gamblingviva levitrawomen taking levitradiscount vardenafil onlinelevitra sampleslowest price levitra generic onlinelevitra penslevitra and flomaxpurchase levitra 60 pillslevitra side effectorder Levitra 10 mgalternative to levitraby levitra priceBuy vardenafil Courier shippinguk alternative vardenafillevitra sarasota floridaprice on levitralevitra or viagra which is betterlevitra from canadalevitra ad reviewtry levitra for freeacheter du levitrawholesale vardenafillevitra where to buycomparison levitra cialistoo much vardenafilcomparison of levitra and viagradosage for levitrageneric mexican levitrahow to buy levitragenuine vardenafilcialis levitra viagra which is bettervardenafil onlinerelative costs of cialis levitraget vardenafil drug onlinelevitra vs viagra vs cialisorder levitra 30 pillssample of vardenafildiscount cialis levitra online canadanatural equivelant ingredient in levitraorder levitravardenafil 20 pills onlinecialis vardenafilreviews of viagra cialis and levitraeuropean vardenafilcanada in levitraPurchase Generic vardenafil 20 mglevitra 100mglevitra propafenoneinfo levitrauk levitra saleswoman\s levitraOnline Levitra 120 pillspurchase vardenafil 10 mggeneric levitra tab 20mgvardenafil australiacompare viagra to levitravardenafil 6 free samplesvardenafil canadian drugslevitra generikaOnline Levitra 20 mgwhat does vardenafil dovardenafil uterine thicknesslevitra hourslevitra cost of salesGeneric Levitra 10 mg ordervardenafil lengthvardenafil sideviagra cyalis levitra comparedlevitra pictureslevitra for salevardenafil online cheapLevitra 20 pills BuyLevitra Free Airmail shipping onlinebuy levitra online atgeneric levitra manufacturermaximum dosage levitravardenafil Courier shipping SaleSale vardenafil 10 pillswhat is better levitra or viagradifference between cialis levitra viagralevitracialis compare levitramedical drug levitravardenafil patent levitraprice levitralevitra tablevardenafil vs cialisvardenafil 180 pills Buyeffects of levitraget levitra drug onlinedreampharmaceuticals from levitranew erectile drug vardenafilover the counter levitrabuy levitra australiangeneric vardenafil indiacialis dysfunction erectile levitralevitra side effects visiondiscount levitra pillsOnline vardenafil Free Insurancecanadian pharmacy levitralevitra low costlevitra reviewsPurchase Generic Levitranew erectile drug levitravardenafil usevardenafil pricinglevitra herb alternativeOrder Generic vardenafil 20 mgvardenafil max complaintsfree vardenafil sampleslevitra or viagraSale Levitra 10 mglevitra lowest pricecheap vardenafil siproblems with levitralevitra suppliers in the ukwatermelon levitralevitra to buy new zealandmedicine levitralevitra 10 mg tabletbuy levitra online dream pharmaceuticalsoft levitra mastercardlevitra ingredientsvardenafil perscription onlinegeneric levitra cheapcanadian vardenafillevitra and price listvardenafil hcl 20 mgbayer levitra onlinefree trial of vardenafilbuy levitra delived fed exrecreational levitra usekey buy levitra onlinelevitrabuy cheap levitra onlineGeneric vardenafil 10 mg onlineorder cheap levitra faslevitra by bayervardenafil 30 pills onlineship free vardenafil sampleregalis vardenafilbuy levitra usvardenafil super activeLevitra 180 pills Buydrug levitra newvardenafil purchasevardenafil 30 pills orderprofessional levitra627 discount levitra online 903levitra and premature ejaculationpfizer vardenafilcialis levitra or viagralevitra twice a daysnorting levitraorder 50mg levitrabuy Levitra 10mgLevitra pills Buybuy cheap levitra onlinelevitra vs cialis reviewbrand name levitratake levitralevitra pill pricevardenafil optionslevitra cialis for womenhow to get levitravardenafil alternativespharmacy vardenafilbuy levitra ukvardenafil hcl 20mg tab side effectsbuy levitra in ukgeneric levitra professionallevitra efectosgeneric levitra effectiveorder vardenafil 10 mg2buy levitra online viagralevitra worksflomax and levitraSale Levitra Airmail shippingclinical data levitrageneric levitra soft tab fasti have levitrarx levitravardenafil 120 pillsbuy vardenafil meds onlinevardenafil online no prescriptionis vardenafil professional realcheap vardenafil walmartuk levitrabuy levitra in canadavardenafil from indiafree sample pack of levitramature vardenafilhow long is levitra effectivelevitra price comparisonlevitra storyvardenafil effekter biverkningarvardenafil for teenscheapest levitra in ukfda on levitralevitra fedexgeneric vardenafil mexicosample levitralevitra best resultslevitra substitutelevitra generic soft tabsafe maximum levitra dosagelevitra online cheapLevitra pills onlinevardenafil free sampleslevitra dysfunction erectilelevitra rxlevitra from mexicovardenafil drug prescriptionvardenafil discountsOrder vardenafil Free Insurancelevitra precautionsbuy buy cheap cheap levitra levitralevitra v s levitravardenafil sex dominationlevitra success storieslevitra for womanvardenafil 20 mg paypallevitra sverigecheap levitra overnightOrder Levitra Airmail shippingbuy vardenafil 10 pillscounterfeit levitrabuy levitra 30 pillsvardenafil suppliers in the ukvardenafil effective timeLevitra Courier shipping orderorder levitra from canadabuy drug satellite tv levitralevitra 20order vardenafillevitra wholesale onlinelevitra fast overnight delivery usa onlyuser review levitracialis and levitra generic brandgeneric levitralevitracialis generic levitra viagralevitra online usdoes levitra look likebuy levitra lowest pricescompare viagra levitralevitra on linesample of levitraviagra cialis levitra candian pharmacy safecheap vardenafil nzprice of vardenafilbuy online vardenafilnatural levitrageneric levitra pillsnatural equivalent ingredient in vardenafilordering vardenafillevitra free pillsbuy vardenafil in uklevitra order2buy generic levitralevitra pay with paypalvardenafil in uk onlineglaxo levitravardenafil and commercialvardenafil 50 mglevitra with lisinoprilwhat does levitra dobuy levitra medicationlevitra couchcan a woman take levitravardenafil 10mgreal levitralevitra offervardenafil 10 pills Buylowest prices for levitracanada vardenafilfree levitra pillscheapest online cost for levitravardenafil generic pricevardenafil gelvardenafil mexicovardenafil ukon levitralevitra discussiongeneric name of levitrabuy vardenafil 30 pillslevitra price walmartlevitra time effectiveLevitra Free Airmail shipping Salevardenafil wholesale onlinelevitra andacheter levitra francecheap vardenafil 20 mgwoman in levitra commercialpill identification levitrageneric discount levitralevitra 20 mg dosagelevitra interactionsoriginal levitrapurchase vardenafil 10mgbuy levitra online in 24 hoursvardenafil for sale without a prescriptionlevitrabuy levitra onlineLevitra 10 mg onlineviagra medication prescription levitra cialis propecialevitra anwendungviagra cialis levitra vpxlcan woman take levitrabuy levitra upsLevitra 90 pills BuySale vardenafil pillovernight vardenafilvardenafil reviewslevitra drugswatermellon vardenafilbuy levitra without prescriptionquery lowest levitra price onlinebuying levitra onlinelevitra generic priceusing levitralevitra for freegeneric viagra levitra generic cialis pillsvardenafil testimonialGeneric Levitra 20 mg ordervardenafil on linevardenafil Free Airmail shipping onlinehow long will levitra lastq buy levitraorder discount levitraviagra levitra cialis offerslevitra and bayercheap pharmacy viagra cialis levitralevitra vs viagra reviewswomans levitraburing levitra onlinelevitra softbuy cheap levitralevitra drug prescriptionbuy levitra in englandvardenafil Salevardenafil for daily useactress in levitra commercialbuy levitra with pay palcheap vardenafil overnightindia vardenafilvardenafil pill orderbuy levitra online 35008 buylevitra pill cuttervardenafil sizevardenafil uk cost pillvardenafil 60 pills onlinegeneric soft tabs vardenafilkey buy levitra cheaplevitra cheap buy onlinelow price vardenafilcheap vardenafil tabletslevitra onlinevardenafil 20 mg orderblindness cialis levitravardenafil levitra onlinelevitra faqsplitting levitravardenafil in ukvardenafil lowest priceprozac interactions with viagra cialis levitrakeywords cialis levitra sales viagrahow quick does levitra workwhat is levitra productPurchase vardenafil Free Airmail shippingLevitra 20 mgcomparison of cialis levitra and viagraovernight shipping of professional vardenafilbenefits of levitralevitra vs viagra which is better2buy cheap levitra onlinelevitra prices american pharmaciesvardenafil online overnightdiscount levitra cialis viagrabuy cheap levitra xanax xenicalorder levitra onlinecheapest vardenafil substitute vardenafilbuy vardenafil cialasbuy cheap generic levitra pharmacy onlinelevitra soft tablevitra third quarter 2004 saleslevitra bestellenvardenafil fedexviva levitra commercialLevitra Courier shipping Salecheapest vardenafil professionalLevitra 10 pills Buycheap vardenafil canadaOnline Levitra Free Airmail shippingfull information levitradiscounted vardenafillevitra substitutesLevitra 20 mg Buylevitra 20 mg bayerbuy vardenafil delived fed exforeign cialis levitra pillswomen vardenafilvardenafil overnight shippingcan women use levitravardenafil Buyvardenafil propafenonelevitra provardenafil impotence drug eli lilly cobuy levitra viagralevitra web siteship free levitra sampleorder levitra without prescriptionfree levitra onlinesuper levitralevitra in ukcompare viagra 26 levitra2cheap levitra onlineOrder vardenafilvardenafil Courier shipping orderbuy vardenafilvardenafil storieslevitra successdiscount levitra purchasecheapest levitra substitute sildenafillevitra cialis viagra comparebuy vardenafil online in ukLevitra 20 mg onlineordering levitraconnecticut levitra vardenafillevitra productsLevitra Free Airmail shipping BuyGeneric vardenafil 10 mg Sale
intense creativity » Tutorial - Playing Video with SubTitles in AS3 - Part 1

Tutorial - Playing Video with SubTitles in AS3 - Part 1

Hi Everyone,

The series on Playing FLV files in AS3 was probably the most popular post on this blog. So I thought I would take it one step further: show SubTitles from an .SRT file over the FLV while it is playing.

First, you need to read this post by Jankees van Woezik. He did a great job at making a SRT parser, although; well, I wouldn't convert everything right away but rather do it on-demand; but that is a small detail.

Then you need to find yourself a SRT file. There are plenty on the internet, I took the Star Wars subtitles, changed the time a little bit, and ended up with this SRT file.

The video I chose I got using TubeGrip, it is basically a Telemark skiing video, which I love and miss. This video is a mp4 format using H.264 codec, which is supported by Flash Player 10 and over. Much more pretty. You can get those from TubeGrip off YouTube.

Now, that being said, I will use

  • a flash.display.Video object to display Video
  • a newcommerce.media.FLVPlayer object to control & play the Video
  • a nl.inlet42.data.subtitles.SubTitleParser to parse the SRT file
  • a flash.net.URLLoader object to load the .srt file
  • a an array of nl.inlet42.data.subtitles.SubTitleData objects where the subtitle data is stored in a usable form

We need to create a component, or Control as I like to call them, which will take SubTitleData objects one at a time and display it.

SubTitleViewer
This component needs to do the following:

  • Position itself appropriately on the stage, taking for granted the video is taking full stageWidth & stageHeight.
  • Show a dark background on which a textfield containing white text will appear
  • Render the subtitle text appropriately

First, I derive this control from newcommerce.controls.BaseControl which I started to dissect here but unfortunately never finished.. (I will one day; hopefully). Basically it implements a frame to work in, which streamlines the main operations you need to deal with when making a visual component.

Then for the creation of the control, I use the example.SubtitleViewerProperties class which contains all the information needed to create the component. Here is the source:

Actionscript:
  1. package example
  2. {
  3.     import flash.display.Stage;
  4.     import flash.text.Font;
  5.     /**
  6.      * ...
  7.      * @author Martin Legris
  8.      */
  9.     public class SubtitleViewerProperties
  10.     {
  11.         protected var _stage:Stage;
  12.         protected var _width:Number;
  13.         protected var _height:Number;
  14.         protected var _font:Font;
  15.        
  16.         public function get font():Font { return _font; }
  17.         public function get height():Number { return _height; }
  18.         public function get width():Number { return _width; }
  19.         public function get stage():Stage { return _stage; }
  20.        
  21.         public function SubtitleViewerProperties(stage:Stage, width:Number = 200, height:Number = 90, font:Font = null)
  22.         {
  23.             _stage = stage;
  24.             _width = width;
  25.             _height = height;
  26.             _font = font;
  27.         }
  28.        
  29.     }
  30. }

So basically, Font, stage reference, width and height.

SubtitleViewer Code
Description is inside of the code. Please read comments for details..

Actionscript:
  1. package example
  2. {
  3.     import ascb.drawing.Pen;
  4.     import flash.display.Shape;
  5.     import flash.display.Sprite;
  6.     import flash.display.Stage;
  7.     import flash.text.Font;
  8.     import flash.text.TextField;
  9.     import flash.text.TextFieldAutoSize;
  10.     import flash.text.TextFormat;
  11.     import flash.text.TextFormatAlign;
  12.     import newcommerce.controls.BaseControl;
  13.     import nl.inlet42.data.subtitles.SubTitleData;
  14.    
  15.     /**
  16.      * ...
  17.      * @author Martin Legris
  18.      */
  19.     public class SubtitleViewer extends BaseControl
  20.     {
  21.         // properties
  22.         protected var _props:SubtitleViewerProperties;
  23.  
  24.         // container, to contain the textfield and has a mask applied, for eventual tweening between subTitltes
  25.         protected var _container:Sprite;
  26.  
  27.         // two textfields, for eventual tweening..
  28.         protected var _oldField:TextField;
  29.         protected var _curField:TextField;
  30.  
  31.         // a bg shape, that contains the dark semi-transparent area underneath the subtitles
  32.         protected var _bg:Shape;
  33.         // the mask, to make sure text doesn't come out of the drawn bg..
  34.         protected var _mask:Shape;
  35.        
  36.         // the current subtitledata
  37.         protected var _curSub:SubTitleData;
  38.        
  39.         public function get props():SubtitleViewerProperties { return _props; }  
  40.         public function set props(value:SubtitleViewerProperties):void { _props = value; }
  41.        
  42.         // constructor takes a SubtitleViewerProperties instance, that contains the parameters needed for construction.
  43.         public function SubtitleViewer(props:SubtitleViewerProperties)
  44.         {
  45.             super({_width:props.width, _height:props.height, props:props});
  46.         }
  47.        
  48.         // init function called at construction time before the creation of children
  49.         override protected function init():void
  50.         {
  51.             _props.stage.addChild(this);
  52.             super.init();
  53.         }
  54.        
  55.         // this is where the visual childs are created
  56.         override protected function createChilds():void
  57.         {
  58.             super.createChilds();
  59.            
  60.             // first create the bg
  61.             _bg = new Shape();
  62.             addChild(_bg);
  63.             // I like to name all displayObjects appropriately..
  64.             _bg.name = "bg";
  65.  
  66.             // create the container   
  67.             _container = new Sprite();
  68.             _container.name = "container";
  69.             addChild(_container);
  70.            
  71.             // create the mask
  72.             _mask = new Shape();
  73.             _mask.name = "mask";
  74.             addChild(_mask);
  75.            
  76.             // apply the mask
  77.             _container.mask = _mask;
  78.            
  79.             // create the two textfields
  80.             createField("curField");
  81.             createField("oldField");
  82.            
  83.             // put fake text... for testing purposes.
  84.             createFakeContent();
  85.         }
  86.        
  87.         // this function creates a textField using the font in the _props member of type SubTitleViewerProperties.
  88.         protected function createField(name:String = ""):TextField
  89.         {
  90.             // create the textfield
  91.             var field:TextField = new TextField();
  92.  
  93.             // add it to the container
  94.             _container.addChild(field);
  95.  
  96.             // name it
  97.             field.name = name;
  98.  
  99.             // assign it to the member property of this class
  100.             this["_" + name] = field;
  101.            
  102.             // we want multi-line text, no auto-sizing, and wordwrap
  103.             field.multiline = true;
  104.             field.autoSize = TextFieldAutoSize.NONE;
  105.             field.wordWrap = true;
  106.            
  107.             // get a reference to the font.. to make code cleaner
  108.             var font:Font = _props.font;
  109.  
  110.             // get the default TextFormat on the newly created textfield
  111.             var tf:TextFormat = field.defaultTextFormat;
  112.            
  113.             // if we did get a font, use it's name & style + make the TextField use embedded fonts
  114.             if (font != null)
  115.             {
  116.                 tf.font = font.fontName;
  117.                 tf.bold = (font.fontStyle.toLowerCase().indexOf("bold") != -1);
  118.                 tf.italic = (font.fontStyle.toLowerCase().indexOf("italic") != -1);
  119.                 field.embedFonts = true;
  120.             }
  121.            
  122.             // set text size to 18
  123.             tf.size = 18;
  124.  
  125.             // align text in the center (this seems to be the standard for subtitles..)
  126.             tf.align = TextFormatAlign.CENTER;
  127.  
  128.             // we want white colored subtitles
  129.             tf.color = 0xFFFFFF;
  130.            
  131.             field.defaultTextFormat = tf;
  132.             try { field.setTextFormat(tf, 0, field.text.length); } catch (e:RangeError) { };
  133.            
  134.             return field;
  135.         }
  136.        
  137.         // simple function to create fake content.. just put text in the _curField and set it's height
  138.         protected function createFakeContent():void
  139.         {
  140.             _curField.text = "my text is original";
  141.             _curField.height = _curField.textHeight + 5;
  142.         }
  143.        
  144.         // function called to position the child elements inside of the component.. usually called at resize
  145.         override protected function arrange():void
  146.         {
  147.             super.arrange();
  148.  
  149.             // position the viewer on the stage, in the middle (x axis) and 80 pixel from the bottom (y axis)
  150.             x = (stage.stageWidth - _width) / 2;
  151.             y = (stage.stageHeight - _height - 80);
  152.            
  153.             // position the textfield, leaving a 7 pixel padding at the top, bottom, left, right
  154.             _curField.x = 7;
  155.             _oldField.x = 7;
  156.             _curField.width = _width - 14;
  157.             _oldField.width = _width - 14;
  158.         }
  159.        
  160.         // function called to redraw the content of the component.. usually when it is resize & at creation time
  161.         override protected function redraw():void
  162.         {
  163.             super.redraw();
  164.            
  165.             // draw the bg
  166.             drawBg();
  167.  
  168.             // draw the mask
  169.             drawMask();
  170.         }
  171.        
  172.         // I don't think I need to explain this..
  173.         protected function drawBg():void
  174.         {
  175.             var p:Pen = new Pen(_bg.graphics);
  176.             p.clear();
  177.             p.lineStyle(0, 0, 0);
  178.             p.beginFill(0x000000, 0.5);
  179.             p.drawRoundRect(0, 0, _width, _height, 9);
  180.             p.endFill();
  181.         }
  182.        
  183.         protected function drawMask():void
  184.         {
  185.             var p:Pen = new Pen(_mask.graphics);
  186.             p.clear();
  187.             p.lineStyle(0, 0, 0);
  188.             p.beginFill(0x000000, 1);
  189.             p.drawRoundRect(0, 0, _width, _height, 9);
  190.             p.endFill();
  191.         }
  192.        
  193.         // function which sets the SubTitleData
  194.         public function setSubTitle(sub:SubTitleData):void
  195.         {
  196.             // check to see if it is a new one
  197.             if (_curSub != sub)
  198.             {
  199.                 // if it is, keep a reference to it
  200.                 _curSub = sub;
  201.  
  202.                 // then render it's text
  203.                 renderSubTitle();
  204.             }   
  205.         }
  206.  
  207.         // render the text
  208.         protected function renderSubTitle():void
  209.         {
  210.             // make sure it isn't null
  211.             if (_curSub != null)
  212.             {
  213.                 // assign the text to the currentField
  214.                 _curField.text = _curSub.text;
  215.  
  216.                 // show the background (not used yet)
  217.                 show();
  218.             }
  219.             else
  220.             {
  221.                 // otherwise set text to nothing.. and hide
  222.                 _curField.text = "";
  223.                 hide();
  224.             }
  225.         }
  226.        
  227.         protected function show():void
  228.         {
  229.            
  230.         }
  231.        
  232.         protected function hide():void
  233.         {
  234.            
  235.         }
  236.     }
  237. }

There we got for part 1. Second part (tomorrow), I will show how to put it all together. Full source code is here if you are interested..

cheers!

Martin

2 Responses to “Tutorial - Playing Video with SubTitles in AS3 - Part 1”

  1. jankees says:

    Thanks for mentioning me.. I am glad you could use my code.

  2. Blog: Robbie Williams & Nicole Kidman Somethin' Stupid with subtitles | Nicole Kidman Celebrity Monitor says:

    [...] intense creativity ยป Tutorial – Playing Video with SubTitles in … [...]

Leave a Reply