Receiving Captionate Embedded Data (II) March 4, 2010. Last updated March 4, 2010.
 
Introduction
First article with the same title was written about 4 years ago. At that time, Flash CS3, which officially introduced AS version 3 was still a year away. In this article, we will see that receiving Captionate embedded events using AS3 is actually very similar to AS2.
 
Note that this is low-level code. Most of the time you will want to use a high-level components like FLVPlayback and FLVPlaybackCaptioning.
 
AS2 Code
Lets refresh how it's done in AS2.
 
[1] The Video Symbol
We need a Video symbol on stage. Create it using the New Video... command in the context menu of Library panel, make sure you choose 'ActionScript' option. Drag the new symbol from Library to Stage. Give the symbol an instance name (Following code assumes the instance name is 'video').
 

 
[2] Playing the FLV
We just need a few lines to play an external FLV file. Enter the following code to the first frame of your movie, and 'test.flv' in the same folder with your SWF will play...
	nc = new NetConnection();
	nc.connect(null);
	ns = new NetStream(nc);
	video.attachVideo(ns);
	ns.play("test.flv");
[3] Receiving Events
Now that we have the FLV playing, we need code to receive the FLV meta events. You can attach event handlers to the NetStream object instance, like below:
	ns.onCaption = function(captions,speaker){
	  trace('onCaption event at '+ns.time);	
	}
In certain situations you may need to use ns["onCaption"] = function... syntax.
 
AS3 Code
AS3 code is actually very similar to AS2 code. There are a few changes but most of the time all you need to add is variable definitions and imports when necessary...
 
[1] The Video Symbol

We, again, need a video symbol on stage. You can create it as in AS2 version, or you can create it the AS3 way in ActionScript, like:
	import flash.media.Video;
	
	var video:Video;

	video = new Video();
	addChild(video);
[2] Playing the FLV
Again the following code is very similar to AS2 code. Main difference is that 'attachVideo' is now called 'attachNetStream' which is more correct:
	import flash.net.NetConnection;
	import flash.net.NetStream;

	var nc:NetConnection;
	var ns:NetStream;

	nc = new NetConnection();
	nc.connect(null);
	ns = new NetStream(nc);
	video.attachNetStream(ns);
	ns.play("test.flv");
[3] Receiving Events
With AS3, event handlers are not attached to NetStream directly, but you will attach them to the client property of NetStream. You can create a custom class (see Flash help for sample code) or you can simply create an object as below:
	ns.client = new Object();
	
	ns.client.onCaption= function(captions:Object,speaker:Number):void{
	  trace('onCaption event at '+ns.time);	
	}
Here is sample code that receives and traces the onMetaData event (in AS3). In Flash, you can create a new AS3 file, copy the code to the fist frame, save the FLA. Make sure you have 'test'flv' in the same folder as you save the FLA.
	//import flash.net.NetConnection;
	//import flash.net.NetStream;
	//import flash.media.Video;
	
	var nc:NetConnection;
	var ns:NetStream;
	var video:Video;
	
	nc = new NetConnection();
	nc.connect(null);
	ns = new NetStream(nc);
	ns.client = new Object();

	video=new Video();
	addChild(video);

	video.attachNetStream(ns);
	ns.play("test.flv");

	function traceObject(info:Object):void{
	  var level;
	  var s;

	  function levelStr(level){
	    s='';
	    for (var n=0;n<level;n++){s=s+'  '};
	    return(s);
	  }

	  function traceThis(x){
	    level++;		
	    for (var y in x){
	      var s=typeof(x[y]);			
	      switch (typeof(x[y])){
	        case 'object':
	          trace(levelStr(level)+y+'('+s+') : '+x[y]);
	          traceThis(x[y]);
	          break;
	        default:
	          trace(levelStr(level)+y+'('+s+') = '+x[y]);
	          break;
	      }
	    }
	    level--;
	  }

	  level=0;
	  traceThis(info);
	}

	ns.client.onMetaData=function(info:Object):void {
	  trace('onMetaData event at '+ns.time);	
	  traceObject(info);
	}

Please send any feedback about this article to support@captionate.com
 
Copyright © 2005-2010 Manitu Group. All rights reserved. All trademarks acknowledged.