Question

I'm trying to create a function that calls random videos from an XML playlist in AS3 and have no idea where to start. I've tried multiple methods and am breaking my brain. Please halp!

although i have had some luck pulling in videos, the randomization doesn't work and I also need to grab the titles from the xml.

<?xml version="1.0" encoding="UTF-8"?>  
<playlist>
        <video url="video/flvs/04-02-02-09-01.flv" title="angry" />    
        <video url="video/flvs/14-02-01-08-01.flv" title="happy"  />    
        <video url="video/flvs/04-02-01-04-01.flv" title="sad"  />
        <video url="video/flvs/06-02-02-03-01.flv" title="frustrated"  />
        <video url="video/flvs/21-02-02-08-01.flv" title="no emotion"  />
</playlist>

AS3:

function Init():void{
    _urlRequest = new URLRequest("playlist.xml");
    _xmlLoader = new URLLoader();
    _xmlLoader = new URLLoader(_urlRequest);
    _xmlLoader.addEventListener(Event.COMPLETE, XMLLoaded, false, 0, true);
}

function XMLLoaded($e:Event):void {
    _xml = new XML($e.target.data);
    var randomVideo:XML = _xml.video[Math.floor(_xml.video.length() * Math.random())];
    PlayVideo();
}
Was it helpful?

Solution

You can access a random playlist node like this (assuming your XML is referenced by a variable called "xml"):

var randomVideo:XML = xml.video[Math.floor(xml.video.length() * Math.random())];

You can access the attributes like this:

var randomURL:String = randomVideo.@url;
var randomTitle:String = randomVideo.@title;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top