I’ve pieced together an XML driven playlist for videos. Only thing I can’t figure out is how to play the playlist continuously. When the last video ends, I want it to start with the first video and run through the playlist again and infinite number of times. I can only get the last video to restart when it finishes instead of going back to the first video.
Much appreciation for any help in resolving this mineute issue. I included my AS below.
// The amount of time between calls to update the playhead timer, in milliseconds.
var updatePlayheadInterval:uint = 10;
// The client object to use for the NetStream object.
var client:Object;
// The index of the currently playing video.
var idx:uint = 0;
// A copy of the current video's metadata object.
var meta:Object;
var nc:NetConnection;
var ns:NetStream;
var playlist:XML;
var t:Timer;
var xmlLoader:URLLoader;
var vid:Video;
var videoXML:XMLList;
// Initialize the xmlLoader variable which will be used to load the external playlist XML file.
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlCompleteHandler);
xmlLoader.load(new URLRequest("xml/videos.xml"));
// Once the XML file has loaded, parse the file contents into an XML object, and
// create an XMList for the video nodes in the XML.
function xmlCompleteHandler(event:Event):void {
playlist = XML(event.target.data);
videoXML = playlist.video;
main();
}
// The main application.
function main():void {
stage.align = StageAlign.TOP_LEFT;
// Create the client object for the NetStream, and set up a callback handler for
// the onMetaData event.
client = new Object();
client.onMetaData = metadataHandler;
nc = new NetConnection();
nc.connect(null);
// Initialize the NetSteam object, add a listener for the netStatus event, and set the
// client for the NetStream.
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
ns.client = client;
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
// Begin playback of the first video.
playVideo();
// Initialize the Timer object and set the delay to updatePlayheadInterval milliseconds.
t = new Timer(updatePlayheadInterval);
t.addEventListener(TimerEvent.TIMER, timerHandler);
}
// Event listener for the ns object. Called when the net stream's status changes.
function netStatusHandler(event:NetStatusEvent):void {
try {
switch (event.info.code) {
case "NetStream.Play.Start" :
//If the current code is Start, start the timer object.
t.start();
break;
case "NetStream.Play.StreamNotFound" :
case "NetStream.Play.Stop" :
// If the current code is Stop or StreamNotFound, stop the timer object and play
// the next video in the playlist.
t.stop();
playNextVideo();
break;
}
} catch (error:TypeError) {
//Ignore any errors.
}
}
// Event listener for the ns object's client property. This method is called when
// the net stream object receives metadata info for a video.
function metadataHandler(metadataObj:Object):void {
// Store the metadata information in the meta object.
meta = metadataObj;
// Resize the video instance on the display list with the video's width and
// height from the metadata object.
vid.width = meta.width;
vid.height = meta.height;
// Center video instance on Stage.
vid.x = (stage.stageWidth - vid.width) / 2;
//vid.y = (stage.stageHeight - vid.height) / 2;
}
// Retrieve the current video from the playlist XML object.
function getVideo():String {
return videoXML[idx].@url;
}
// Play the currently selected video.
function playVideo():void {
var url:String = getVideo();
ns.play(url);
}
// Decrease the current video index and begin playback of the video.
function playPreviousVideo():void {
if (idx > 0) {
idx--;
playVideo();
}
}
// Increase the current video index and begin playback of the video.
function playNextVideo():void {
if (idx < (videoXML.length() - 1)) {
// If this is not the last video in the playlist increase the video index
// and play the next video.
idx++;
playVideo();
} else {
// Instruct what to do after end of last video is reached.
//removeChild(vid);
}
}
// Event handler for the timer object. This method is called every
// updatePlayheadInterval milliseconds as long as the timer is running.
function timerHandler(event:TimerEvent):void {
try {
} catch (error:Error) {
// Ignore this error.
}
}