Description
This is a sample of some of my ActionScript 3.0 code. This is a simple video widget that will either connect up with a Flash Media Server to stream synchronous or asynchronous video, or it will play a local file as a progressive download. In this sample, I connect to a local file, as I don't own a Flash Media Server. The sample code is from the MediaHolder.as file. The video is a wireframe bust rendering of a model created in XSI and encoded to f4v.
Example
Code Sample:
package com.trevolution {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.text.*;
public class MediaHolder extends Sprite {
// All the test paths and asset locations on a Flash Media Server
private var path:String = "rtmp://";
private var asset:String = "";
// The various places for establishing connections and playing streams
private var connection:NetConnection;
private var stream:NetStream;
private var video:Video;
// The areas/holders for all the layers of the video player
private var videoArea:Sprite = new Sprite();
private var screenArea:Sprite = new Sprite();
private var buttonArea:Sprite = new Sprite();
// The overlay for when the video is not playing...
private var screen:Shape = new Shape();
private var client:Object = { onMetaData:metaDataHandler };
///////////////////////////////////////////////////////////
//
// MediaHolder Constructor
//
//
public function MediaHolder() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
connection = new NetConnection();
connection.objectEncoding = ObjectEncoding.AMF0;
connection.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
// If using a Flash Media Server use path
// connection.connect( path );
// If not, use null
connection.connect( null );
//
buildScreenArea();
buildButtonArea();
// Add the areas to the Stage
addChild( videoArea );
addChild( screenArea );
addChild( buttonArea );
screenArea.visible = false;
buttonArea.buttonMode = true;
videoArea.buttonMode = true;
screenArea.buttonMode = true;
// Add Event Listeners for Mouse events
videoArea.addEventListener( MouseEvent.MOUSE_OVER, mouseEventHandler );
videoArea.addEventListener( MouseEvent.MOUSE_OUT, mouseEventHandler );
buttonArea.addEventListener( MouseEvent.MOUSE_OVER, mouseEventHandler );
buttonArea.addEventListener( MouseEvent.MOUSE_OUT, mouseEventHandler );
doDrawGradient();
}
///////////////////////////////////////////////////////////
//
// Functions
//
//
private function doDrawGradient() {
var testArea:Sprite = new Sprite();
var buttonW:uint = 60;
var buttonH:uint = 30;
var ellipses:Array = [3, 3, 3, 3]
var test:MediaButton = new MediaButton( buttonW, buttonH, ellipses );
testArea.addChild( test );
addChild( testArea );
test.addEventListener( MouseEvent.MOUSE_DOWN, mouseEventHandler );
testArea.x = 330;
testArea.y = 10;
}
private function connectStream():void {
stream = new NetStream( connection );
stream.client = client;
stream.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
stream.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
video = new Video();
video.attachNetStream( stream );
stream.play( asset );
// for testing, the volume is defaulted to off
setVolume( 0.0 );
videoArea.addChild( video );
}
private function buildScreenArea() {
screen.graphics.beginFill( 0x000000, 0.5 );
screen.graphics.drawRect( 0, 0, 320, 240 );
screen.graphics.endFill();
var textArea:TextField = new TextField();
textArea.autoSize = TextFieldAutoSize.LEFT;
textArea.embedFonts = true;
textArea.border = true;
textArea.borderColor = 0xffffff;
textArea.text = "textArea message";
var format:TextFormat = new TextFormat();
format.font = "ArialFont";
format.color = 0x336699;
format.size = 12;
textArea.defaultTextFormat = format;
screenArea.addChild( screen );
screenArea.addChild( textArea );
}
private function buildButtonArea() {
buttonArea.graphics.beginFill( 0x000000, 0.5 );
buttonArea.graphics.drawRect( 0, 0, 300, 50 );
buttonArea.graphics.endFill();
}
///////////////////////////////////////////////////////////
//
// Get/Set Functions
//
//
private function setVolume( amount:Number ):void {
var sound:SoundTransform = new SoundTransform( amount, 0 );
stream.soundTransform = sound;
}
///////////////////////////////////////////////////////////
//
// Event Handlers
//
//
private function netStatusHandler( statusEvent:NetStatusEvent ):void {
trace( statusEvent.info.code );
switch( statusEvent.info.code ) {
// NetConnection Handlers
case "NetConnection.Connect.Success":
connectStream();
break;
// NetStream Handlers
// The state of the NetStream dictates which areas are visible and which are not
case "NetStream.Play.Start":
screenArea.visible = false;
break;
case "NetStream.Play.Stop":
screenArea.visible = true;
break;
case "NetStream.Play.StreamNotFound":
break;
case "NetStream.Pause.Notify":
screenArea.visible = true;
break;
case "NetStream.Unpause.Notify":
screenArea.visible = false;
break;
}
}
private function mouseEventHandler( mouseEvent:MouseEvent ):void {
switch( mouseEvent.type ) {
case "mouseDown":
stream.togglePause();
break;
}
}
private function metaDataHandler( metaData:Object ):void {
var areaIndent:uint = 10;
video.width = metaData.width;
video.height = metaData.height;
screen.width = video.width;
screen.height = video.height;
buttonArea.x = ( video.width - buttonArea.width ) / 2;
buttonArea.y = ( video.height - buttonArea.height - areaIndent );
}
private function asyncErrorHandler( errorEvent:AsyncErrorEvent ):void {
}
}
}