Net-IT Web Consulting | Melbourne Web Design

Build a free streaming AS3 flash video player

Here is a free AS3 flash video player to stream flv and f4v files on your website. Feel free to modify the source code, fla file and use it how you like.

Free Customisable AS3 Flash Video Player

 


AS3 Source Code

Constants

The number of seconds of video to buffer before starting play

const BUFFER_SECONDS:Number = 8;

The volume of player when unmuted

const VOLUME:Number = 1;

How ofter to update the display duration (in milliseconds)

const TIMER_UPDATE_DELAY:int = 10;

Video smoothing. Set this to false for better performance.

const SMOOTHING:Boolean = true;

Progress bar width: If you modify the width of the progress bar on the stage, adjust this value to match the width.

const PROGRESS_WIDTH = 355;

Autoplay. Set this to true to make the video start automatically.

const VIDEO_AUTOPLAY = true;

 

Variables

var blLoaded:Boolean = false;
var blProgressScrub:Boolean = false;
var ncConnection:NetConnection;
var nsStream:NetStream;
var objInfo:Object;
var strSource:String = "video.flv";
var tmrDisplay:Timer;

 

Functions

function initVideoPlayer():void {

mcVideoControls.btnUnmute.visible = false;
if(VIDEO_AUTOPLAY) mcVideoControls.btnPlay.visible = false;
else mcVideoControls.btnPause.visible= false;

mcVideoControls.mcProgressFill.mcFillBlue.width = 1;
mcVideoControls.mcProgressFill.mcFillGrey.width = 1;

stage.addEventListener( MouseEvent.MOUSE_UP, mouseReleased);

mcVideoControls.btnPause.addEventListener(MouseEvent.CLICK, pauseClicked);
mcVideoControls.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);
mcVideoControls.btnMute.addEventListener(MouseEvent.CLICK, muteClicked);
mcVideoControls.btnUnmute.addEventListener(MouseEvent.CLICK, unmuteClicked);
mcVideoControls.mcProgressScrubber.btnProgressScrubber.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);

tmrDisplay = new Timer(TIMER_UPDATE_DELAY);
tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);

ncConnection = new NetConnection();
ncConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
ncConnection.connect(null);

nsStream = new NetStream(ncConnection);
nsStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nsStream.client = this;
nsStream.bufferTime = BUFFER_SECONDS;

vidDisplay.attachNetStream(nsStream);
vidDisplay.smoothing = SMOOTHING;
setVolume(VOLUME);

if(VIDEO_AUTOPLAY) nsStream.play(strSource);
}

function playClicked(e:MouseEvent):void {

if(!blLoaded) {
nsStream.play(strSource);
blLoaded = true;
} else {
nsStream.resume();
}

vidDisplay.visible = true;

mcVideoControls.btnPause.visible = true;
mcVideoControls.btnPlay.visible = false;
}

function pauseClicked(e:MouseEvent):void {

nsStream.pause();
mcVideoControls.btnPause.visible = false;
mcVideoControls.btnPlay.visible = true;
}

function muteClicked(e:MouseEvent):void {

setVolume(0);
}

function unmuteClicked(e:MouseEvent):void {

setVolume(VOLUME);
}

function progressScrubberClicked(e:MouseEvent):void {

blProgressScrub = true;
mcVideoControls.mcProgressScrubber.startDrag(false, new Rectangle(mcVideoControls.mcProgressContainer.x-10, (mcVideoControls.mcProgressContainer.y-1), (PROGRESS_WIDTH-mcVideoControls.mcProgressScrubber.width), 0));
}

function mouseReleased(e:MouseEvent):void {

blProgressScrub = false;
mcVideoControls.mcProgressScrubber.stopDrag();
mcVideoControls.mcProgressFill.mcFillBlue.width = mcVideoControls.mcProgressScrubber.x + mcVideoControls.mcProgressScrubber.width;
}

function updateDisplay(e:TimerEvent):void {

if(blProgressScrub) {
nsStream.seek(Math.round((mcVideoControls.mcProgressScrubber.x-mcVideoControls.mcProgressContainer.x) * objInfo.duration / (PROGRESS_WIDTH-30)))
} else {
mcVideoControls.mcProgressScrubber.x = mcVideoControls.mcProgressContainer.x-10 + nsStream.time * (PROGRESS_WIDTH-25) / objInfo.duration;
}
mcVideoControls.lblTimeDuration.htmlText = "<font color='#ffffff'>" + formatTime(nsStream.time) + "</font> / " + formatTime(objInfo.duration);

mcVideoControls.mcProgressFill.mcFillBlue.width = mcVideoControls.mcProgressScrubber.x + mcVideoControls.mcProgressScrubber.width - mcVideoControls.mcProgressContainer.x;
mcVideoControls.mcProgressFill.mcFillGrey.width = nsStream.bytesLoaded * PROGRESS_WIDTH / nsStream.bytesTotal;
}

function onMetaData(info:Object):void {

objInfo = info;
tmrDisplay.start();
}

function netStatusHandler(event:NetStatusEvent):void {

switch (event.info.code) {
case "NetStream.Play.StreamNotFound":
trace("Stream not found: " + strSource);
break;

case "NetStream.Play.Stop":
stopVideoPlayer();
break;
}
}

function stopVideoPlayer():void {

nsStream.pause();
nsStream.seek(0);
vidDisplay.visible = false;
mcVideoControls.btnPause.visible = false;
mcVideoControls.btnPlay.visible = true;
}

function setVolume(intVolume:Number = 0):void {

var sndTransform = new SoundTransform(intVolume);
nsStream.soundTransform = sndTransform;
if(intVolume > 0) {
mcVideoControls.btnMute.visible = true;
mcVideoControls.btnUnmute.visible = false;
} else {
mcVideoControls.btnMute.visible = false;
mcVideoControls.btnUnmute.visible = true;
}
}

function formatTime(t:int):String {

var s:int = Math.round(t);
var m:int = 0;
if (s > 0) {
while (s > 59) {
m++;
s -= 60;
}
return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);
} else {
return "00:00";
}
}

 

Initialise player

This line starts the player loading.

initVideoPlayer();

Thank you for reading. If you do use this player on your website, feel free to link back to www.net-it.com.au. Use this video player at your own risk. We take no responsibility for any damages caused by this player.