First off, I am not a Flash developer and am sure my code is not great… so I am open to ideas and suggestions.
I have created a AS3 media player that will play video and images (for a duration). The player grabs the files from a xml playlist. I have this working. It will loop through the items and start over once it gets the end.
But what I am stuck on is preloading each item when called. Currently I am not preloading the media item, and depending on the size it may take a few seconds to load.
I created a simple preloader function, but I am confused on how to call the this function on each item load.
AS3 Code
var nodeId:String;
var networkPath:String;
var adItemPath:String;
var hostPathXML:XML = new XML();
var nodePathXML:XML = new XML();
var itemsArray:Array = new Array();
var itemsCounter:int = 0;
var position:Number = 0;
var i:Number = 0;
var adCountMain:Number;
var adTypeMain:String;
var adDuration:int = 0;
var adSrcMain:String;
var video:Video;
var resetPlayer = false;
var image:Loader;
var mySound:SoundTransform;
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.client = this;
function callPHP() {
var request:URLRequest = new URLRequest("http://url here");
request.method = URLRequestMethod.GET;
var variables:URLVariables = new URLVariables();
variables.position = position;
variables.duration = adDuration;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, initializePlayer);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
}
function initializePlayer(evt:Event) {
if (resetPlayer)
{
if (adTypeMain == 'Video')
{
removeChild(video);
}else{
removeChild(image);
}
}else{
resetPlayer = true;
}
adTypeMain = evt.target.data.adType;
adSrcMain = evt.target.data.adSrc;
adDuration = evt.target.data.duration;
position = evt.target.data.position;
setMain();
}
//Create Video and Image Player
function setMain() {
if (adTypeMain == "Video") {
video = new Video();
video.width = 1046;
video.height = 588;
addChild(video);
video.attachNetStream(stream_ns);
stream_ns.play(adSrcMain);
}
if (adTypeMain == "ImageBased") {
image = new Loader();
image.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);
var newImage:URLRequest = new URLRequest(adSrcMain);
image.load(newImage);
function onLoaderReady(e:Event) {
addChild(image);
image.width = 1046;
image.height = 588;
callPHP();
}
}
}
//Watch for end of Video
stream_ns.addEventListener(NetStatusEvent.NET_STATUS, checkStatus);
function checkStatus(e:NetStatusEvent):void {
if (e.info.code == "NetStream.Play.Stop"){
adDuration = 0;
callPHP();
}
}
callPHP();
Mouse.hide();
Pre loader Function
var l:URLLoader = new URLLoader();
l.addEventListener(ProgressEvent.PROGRESS, loop);
l.addEventListener(Event.COMPLETE, loadVideo);
l.load(new URLRequest(movie));
function loop(e:ProgressEvent):void {
var perc:Number = e.bytesLoaded / e.bytesTotal;
percent.text = Math.ceil(perc*100).toString();
}
function loadVideo(e:Event):void { //Video Player
removeChildAt(0);
percent = null;
video = new Video();
video.width = 1000;
video.height = 270;
addChild(video);
video.attachNetStream(stream_ns);
stream_ns.play(movie);
}
Any help would be great!
Thanks,