Archive for August, 2009

Simple Actionscript 3.0 Flash VideoPlayer in < 40 lines of code

So.. you want a flash videoplayer but don’t want to purchase an expensive player, or use the great multi-i-can-play-everything-player which is 30kb+ in swf size?

Lets check this out how easy it could be to really create a tiny videoplayer, the following code does compile into roughly 1kb swf which should be small enough for everyone to use, let the user spend his bandwidth on downloading the actual video instead of the player itself!
Set the flashvar videoUrl to specify which video to play.

See the player in action here

package 
{
	import flash.display.*;
	import flash.media.*;
	import flash.events.*;
	import flash.net.*;
	/**
	 * @author Erik Karlsson, www.nonobtrusive.com
	 */
	public class Main extends Sprite 
	{
		public function Main() 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			var connection:NetConnection = new NetConnection();
			connection.connect(null);			
			var stream:NetStream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, onEvent );
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onEvent );
			stream.addEventListener(IOErrorEvent.IO_ERROR, onEvent );
			stream.client = this;     //for onMetaData and onCuePoint
			var video:Video = new Video(stage.stageWidth, stage.stageHeight);
			video.attachNetStream( stream );
			stream.play( stage.loaderInfo.parameters["videoUrl"] );  //pass the url to video as videoUrl flashvar
			addChild(video);
		}
		private function onEvent(event:Event):void {
			trace( event.toString() );		//handle this as you wish
		}
		public function onMetaData(info:Object):void {
			trace( info.duration + " - " + info.width + " - " + info.height + " - " + info.framerate ); //handle this as you wish
		}
		public function onCuePoint(info:Object):void {
			trace( info.time + " - " + info.name + " - " + info.type ); //handle this as you wish
		}
		public function onXMPData(data:Object):void {
                        //used in flash10+
			trace( data ); //handle this as you wish
		}
	}
}

, , ,

71 Comments