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 FLV video in plain AS3 - Part 2

Tutorial - Playing FLV video in plain AS3 - Part 2

Hi Everyone,

today I will continue on this topic:

and the following installments are here:

In the first part I only discussed the essentials for playing an FLV. Today I will show how to build a re-usable class around those basic ideas. At the end of this tutorial you will have a solid set of classes (3) that you can reuse as a base to play FLV videos in all your widgets.

Architecture

There will be three classes involved in the development today:

  • MediaData -- to hold the information on the FLV being played
  • FLVPlayer -- to play the FLV
  • MediaEvent -- to communicate with the outside world as to what is going on inside the FLVPlayer

This is a gross preview of where we are heading at:
diagram of FLVPlayer

MediaData
Will hold the basic information about the FLV to be played: it's height, width, duration, uri, image to represent it, and title

FLVPlayer
Will play the actual FLV, using the MediaData object as input, and dispatch MediaEvent's to inform the outside world of what is going on.

MediaEvent
Hold information to be received by the outside world, updating the status on what is happening in the FLVPlayer

MediaData

This is a simple class, whose role is simply to hold the information on a particular FLV file. It contains:

  • title -- the title of the flv
  • image -- an image to be used to represent the flv, either as a thumbnail or a place holder before the flv loads
  • uri -- the uri of the FLV file itself
  • duration -- the duration in seconds of the flv file
  • width -- the width in pixels of the video
  • height -- the height in pixels of the video
Actionscript:
  1. package newcommerce.media
  2. {
  3.     public class MediaData
  4.     {
  5.         protected var _uri:String;
  6.         protected var _title:String;
  7.         protected var _duration:Number;
  8.         protected var _image:String;
  9.         protected var _width:Number;
  10.         protected var _height:Number;
  11.        
  12.         public function set uri(uri:String):void { _uri = uri; }
  13.         public function get uri():String { return _uri; }
  14.         public function set image(image:String):void { _image = image; }
  15.         public function get image():String { return _image; }
  16.         public function set title(title:String):void { _title = title; }
  17.         public function get title():String { return _title; }
  18.         public function set duration(duration:Number):void { _duration = duration; }
  19.         public function get duration():Number { return _duration; }
  20.         public function get width():Number { return _width; }
  21.         public function set width(value:Number):void { _width = value; }
  22.         public function get height():Number { return _height; }
  23.         public function set height(value:Number):void { _height = value; }
  24.        
  25.         function MediaData(uri:String = "", title:String = "", duration:Number = 0,  image:String = "", width:Number = 320, height:Number = 200)
  26.         {         
  27.             _uri = uri;
  28.             _title = title;
  29.             _duration = duration;
  30.             _image = image;
  31.             _width = width;
  32.             _height = height;
  33.         }
  34.     }
  35. }

FLVPlayer

The FLVPlayer is the core where all the action happens. First let's start with the most important functions:

  • connect() -- to initialize the NetConnect & NetStream objects
  • playMedia(media:MediaData) -- to start playing the media
  • pause() -- to pause the media
  • seek(pos:Number) -- to seek to a certain position

For an explanation of the code down here, please refer to the first part of the tutorial, which covers the basics. Note that if you want efficient loading of FLV's one after the other, where it's possible that the first one is not completely loaded yet when you want the second one to start playing (each new video replacing the old ones) it's important to close the NetStream then the NetConnection, and start over with fresh ones (from my experience).

Actionscript:
  1. package newcommerce.media
  2. {
  3.         import flash.media.SoundTransform;
  4.    
  5.     import newcommerce.media.MediaData;
  6.     import newcommerce.media.MediaEvent;
  7.    
  8.     import flash.net.NetConnection
  9.     import flash.net.NetStream;
  10.     import flash.media.Video;
  11.     import flash.display.Sprite;
  12.  
  13.     import flash.events.IOErrorEvent;
  14.     import flash.events.NetStatusEvent
  15.     import flash.events.SecurityErrorEvent;
  16.     import flash.events.AsyncErrorEvent;
  17.    
  18.     import flash.events.EventDispatcher;
  19.     import flash.utils.Timer;
  20.     import flash.events.TimerEvent;
  21.  
  22.     public class FLVPlayer extends EventDispatcher
  23.     {      
  24.         protected var _currentMedia:MediaData;     
  25.         protected var _stream:NetStream;
  26.         protected var _paused:Boolean = false;
  27.         protected var _playing:Boolean = false;
  28.         protected var _timer:Timer;  
  29.         protected var _soundTransform:SoundTransform;
  30.         protected var _lastVolume:Number = 1;
  31.         protected var _mute:Boolean = false;       
  32.         protected var _connection:NetConnection;       
  33.         protected var _mediaFinished:Boolean = false;      
  34.         protected var _streamStatus:Array;
  35.         protected var _lastPos:Number;
  36.         protected var _video:Video;
  37.  
  38.  
  39.         // constructor will go here
  40.  
  41.         public function connect():void
  42.         {
  43.             // stop the stream if it exists and is playing
  44.             stop();
  45.  
  46.             // build a new NetConnection and NetStream
  47.             newStream();
  48.         }
  49.  
  50.         protected function newStream():void
  51.         {
  52.             // if either the _stream or _connection are open, remove event listeners and close them
  53.             if(_stream != null)
  54.             {
  55.                 _stream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
  56.                 _stream.removeEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  57.                 _stream.removeEventListener(IOErrorEvent.IO_ERROR, doIOError);
  58.                 _stream.close();
  59.             }
  60.        
  61.             if(_connection != null)
  62.             {
  63.                 _connection.removeEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  64.                 _connection.removeEventListener(IOErrorEvent.IO_ERROR, doIOError);
  65.                 _connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
  66.                 _connection.close();
  67.             }
  68.  
  69.             // create a new connection
  70.             _connection = new NetConnection();
  71.             _connection.connect(null);
  72.             _connection.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  73.             _connection.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
  74.             _connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
  75.            
  76.             // create a new stream
  77.             _stream = new NetStream(_connection);
  78.             _stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
  79.             _stream.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
  80.             _stream.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
  81.             _stream.client = this;
  82.  
  83.             // attach the stream to the Video visual component
  84.             _video.attachNetStream(_stream);
  85.  
  86.             // set the bufferTime (in seconds)
  87.             _stream.bufferTime = 1;
  88.  
  89.             // receive audio and video
  90.             _stream.receiveAudio(true);
  91.             _stream.receiveVideo(true);
  92.         }
  93.  
  94.         public function playMedia(media:AbstractMediaData):void
  95.         {
  96.             // used to track when a media has finished playing or not
  97.             _mediaFinished = false;
  98.             if(media == null)
  99.                 return;
  100.            
  101.             _currentMedia = media;
  102.  
  103.             // used to track the status message we received in the doNetStatus function
  104.             _streamStatus = [];
  105.            
  106.             // force a fresh play
  107.             play(true);
  108.         }   
  109.  
  110.         // function to play the _currentMedia, forced means to force it to restart at the beginning, while unforced can serve to "de-pause" a playing FLV
  111.         public function play(forced:Boolean = false):void
  112.         {
  113.             // if the FLV is paused and we are not forcing it to start at the beginning
  114.             if(_paused && !forced)
  115.             {
  116.                 _stream.resume();
  117.                 _paused = false;
  118.                 _timer.start();
  119.                 return;
  120.             }
  121.  
  122.             // reset the NetConnection and NetStream objects (this is important when having a player with a playlist with a SKIP or NEXT button..
  123.             connect();
  124.  
  125.             // officially starts streaming the .FLV file
  126.             _stream.play(_currentMedia.uri);
  127.  
  128.             // set the volume to the _lastVolume value
  129.             setVolume(this._lastVolume*100);
  130.  
  131.             // set the playing flag
  132.             _playing = true;
  133.  
  134.             // set the pause flag
  135.             _paused = false;
  136.  
  137.             // start our timer to track stream position & loading progress
  138.             _timer.start();
  139.         }
  140.  
  141.         public function stop():void
  142.         {
  143.             // if playing flag is on
  144.             if(!_playing)
  145.                 return;
  146.  
  147.             // pause the stream    
  148.             _stream.pause();
  149.  
  150.             // set playing flag to false
  151.             _playing = false;
  152.  
  153.             // stop the timer
  154.             _timer.stop();
  155.         }
  156.  
  157.         public function pause():void
  158.         {
  159.             // pause the stream
  160.             _stream.pause();
  161.  
  162.             // set the paused flag to true
  163.             _paused = true;
  164.  
  165.             // stop the timer
  166.             _timer.stop();
  167.  
  168.             // notice the _playing flag is untouched. when paused, we have _playing = true & _paused = true, this could be simplified, I know.
  169.         }
  170.  
  171.         public function seek(seconds:Number):void
  172.         {   
  173.             // if the requested position is less than the media's known duration (-2 is because FLV's don't like to be seeked near the end), and we are currently _playing
  174.             if (seconds <_currentMedia.duration-2 && _playing)
  175.             {
  176.                 // record the last know valid playing position in case an error happens
  177.                 _lastPos = _stream.time;
  178.  
  179.                 // seek to the requested position
  180.                 _stream.seek(seconds);
  181.  
  182.                 // make sure the stream is playing
  183.                 _stream.resume();
  184.  
  185.                 // unset the pause flag
  186.                 _paused = false;
  187.             }
  188.         }

Now we can tackle the rest...

Actionscript:
  1. public function FLVPlayer()
  2.         {         
  3.             // create a new SoundTransform object, for the volume of the playing FLV
  4.             _soundTransform = new SoundTransform();
  5.  
  6.             // this timer object is used to keep track of the stream position & loading progress
  7.             _timer = new Timer(250);
  8.             _timer.addEventListener(TimerEvent.TIMER, doMediaTime);
  9.  
  10.             // initialize the streamStatus array to keep a small (3 items) log of the last doNetStatus events
  11.             // I analyze this information to understand exactly when a FLV has reached the end since time values cannot be trusted.
  12.             _streamStatus = [];
  13.         }
  14.  
  15.  
  16.         public function mute(muted:Boolean = true):void
  17.         {
  18.             _mute = muted;
  19.             if(_mute)
  20.             {
  21.                 _soundTransform.volume = 0;
  22.                 _stream.soundTransform = _soundTransform;
  23.             }
  24.             else
  25.             {
  26.                 _soundTransform.volume = _lastVolume;
  27.                 _stream.soundTransform = _soundTransform;
  28.             }
  29.         }
  30.        
  31.         public function setVolume(volume:Number):void
  32.         {   
  33.             // keep track of the last set volume for the mute function
  34.             _lastVolume = _soundTransform.volume = volume/100;
  35.            
  36.             // if not muted, apply the requested volume
  37.             if(!_mute)
  38.                 _stream.soundTransform = _soundTransform;                  
  39.         }

and finally the event handling function

Actionscript:
  1. public function onMetaData(infoObject:Object):void
  2.             {
  3.             // if the event contains duration information
  4.             if (infoObject.duration != null)
  5.             {
  6.                 // dispatch a DURATION event
  7.                 dispatchEvent(new MediaEvent(MediaEvent.DURATION_RECEIVED, {duration:infoObject.duration}));
  8.  
  9.                 // set the mediaData's duration
  10.                 _currentMedia.duration = infoObject.duration;
  11.             }
  12.  
  13.             // normally we received the width and height of the video as well
  14.             var lclHeight:Number = infoObject.height;
  15.             var lclWidth:Number = infoObject.width;
  16.  
  17.             // if they are invalid, got to your default values
  18.             if(isNaN(lclHeight) || isNaN(lclWidth))
  19.             {
  20.                 lclWidth = 425;
  21.                 lclHeight= 320;
  22.             }
  23.  
  24.             // dispatch the size received event
  25.             dispatchEvent(new MediaEvent(MediaEvent.SIZE_RECEIVED, { width:lclWidth, height:lclHeight } ));
  26.             }
  27.        
  28.         // function function is on the _timer object, running 4 times per second
  29.         public function doMediaTime(evt:TimerEvent):void
  30.         {
  31.             // calculate the percentage loaded
  32.             var pct:Number = Math.round(_stream.bytesLoaded / _stream.bytesTotal * 100);
  33.            
  34.             // dispatch a TIME event containing the stream position & percentage loaded
  35.             dispatchEvent(new MediaEvent(MediaEvent.TIME, { seconds:_stream.time, loaded:pct } ));
  36.         }
  37.        
  38.         // these error handlers are basic, the listeners can catch then and handle the errors accordingly
  39.         protected function doSecurityError(evt:SecurityErrorEvent):void
  40.         {
  41.             trace("AbstractStream.securityError");
  42.             dispatchEvent(new MediaEvent(MediaEvent.ERROR, { errorEvt:evt } ));
  43.         }      
  44.  
  45.         protected function doIOError(evt:IOErrorEvent):void
  46.         {
  47.             trace("AbstractScreem.ioError");
  48.             dispatchEvent(new MediaEvent(MediaEvent.ERROR, { errorEvt:evt } ));
  49.         }
  50.        
  51.         protected function doAsyncError(evt:AsyncErrorEvent)
  52.         {
  53.             trace("AsyncError");
  54.             dispatchEvent(new MediaEvent(MediaEvent.ERROR, { errorEvt:evt } ));
  55.         }      
  56.        
  57.         // this function handles the Stream's & NetConnection's NetStatus events. They could be seperated
  58.         protected function doNetStatus(evt:NetStatusEvent):void
  59.         {
  60.             // this happens when somebody seeks to close to the end.. if it happens, revert to the last known playing position
  61.             if (evt.info.code == "NetStream.Seek.InvalidTime")
  62.             {
  63.                 _stream.seek(_lastPos);
  64.                 _stream.resume();
  65.             }
  66.             // otherwise push the code into the log and analyze the log
  67.             else
  68.             {
  69.                 pushStatus(evt.info.code);     
  70.                 analyzeStatus();
  71.             }
  72.         }

finally the function to push a status code, and analyze the log

Actionscript:
  1. // add the code to the log, and for the current analysis only the last 3 are needed to dump the others
  2.         protected function pushStatus(status:String):void
  3.         {
  4.             _streamStatus.push(status);
  5.             while (_streamStatus.length> 3)
  6.                 _streamStatus.shift();
  7.         }
  8.  
  9.         // this function analyzes the status code log to find when a media has really finished playing since the time values couldn't be trusted.
  10.         protected function analyzeStatus():void
  11.         {
  12.             // find the position of three important netstatus codes in the log
  13.             var stopIdx:Number = _streamStatus.lastIndexOf("NetStream.Play.Stop");
  14.             var flushIdx:Number = _streamStatus.lastIndexOf("NetStream.Buffer.Flush");
  15.             var emptyIdx:Number = _streamStatus.lastIndexOf("NetStream.Buffer.Empty");
  16.            
  17.             var mediaFinished:Boolean = false;
  18.            
  19.             // then depending if they are there, and in which order they appear, we know when a media has finished playing.
  20.             if (stopIdx> -1 && flushIdx> -1 && emptyIdx> -1)
  21.             {
  22.                 if (flushIdx <stopIdx && stopIdx <emptyIdx)
  23.                 {
  24.                     mediaFinished = true;
  25.                 }
  26.             }
  27.             else if (flushIdx> -1 && emptyIdx> -1)
  28.             {
  29.                 if (flushIdx <emptyIdx)
  30.                     mediaFinished = true;
  31.             }
  32.             else if (stopIdx> -1 && flushIdx> -1)
  33.             {
  34.                 mediaFinished = true;
  35.             }
  36.  
  37.             // if the media has finished playing, dispatch the event and clean the log
  38.             if (mediaFinished)
  39.             {      
  40.                 dispatchEvent(new MediaEvent(MediaEvent.FINISHED_PLAYING, { } ))               
  41.                 while (_streamStatus.length> 0)
  42.                     _streamStatus.pop();
  43.             }
  44.         }

The rest is here: Tutorial - Playing FLV video in plain AS3 - Part 3



Related posts (automatically generated):

  1. Tutorial - Playing FLV video in plain AS3 - Part 1
  2. Tutorial - Playing FLV video in plain AS3 - Part 3 (Updated)
  3. Tutorial - Playing FLV video in plain AS3 - Part 4 - Sample Usage


5 Responses to “Tutorial - Playing FLV video in plain AS3 - Part 2”

  1. den says:

    great tutorial !!!

  2. art says:

    most comprehensive tutorial I have seen on this! any sign of a follow up?

  3. Andy says:

    Great tutorial, do you think you’ll have time to complete the next bit soon? :)

    Keep up the good work!

    Andy

  4. Sven says:

    Will the tutorial on the Event class be finished?

  5. Jingtao says:

    hello, everyone! I am making a flv video player.I want to complete that click anwhere then play at the position but not care if the position in the buffered scope, I use NetStream.seek(minute), but this only userfull int the buffered scope, can anyone help me? thanks .

Leave a Reply