-
xml flv playlist with randomization AS3
I'm trying to create a function that calls random videos from an XML playlist in AS3 and have no idea to randomization. Please help!
XML:
<?xml version="1.0" encoding="UTF-8"?>
<playlist id="Videoplayer" >
<vid desc="ClipTube1"
src="VideoClipTube1.flv" />
<vid desc="ClipTube2"
src="VideoClipTube2.flv" />
<vid desc="ClipTube3"
src="VideoClipTube3.flv" />
</playlist>
AS3:
Code:
public function VideoPlaylistSeq():void
{
// Load the playlist file, then initialize the media player.
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, initMediaPlayer);
xmlLoader.load(new URLRequest("Playlist.xml"));
// Format the tileList, specify its cellRenderer class.;
tileList.setSize(100, 350);
tileList.columnWidth = 80;
tileList.rowHeight = 60;
tileList.direction = ScrollBarDirection.VERTICAL;
tileList.setStyle("cellRenderer", Thumb);
}
public function initMediaPlayer(event:Event):void
{
var myXML:XML = new XML(xmlLoader.data);
//my randomization code??
var item:XML = xmlLoader.data[Math.floor(xmlLoader.data.length() * Math.random())];
for each (item in myXML.vid)
{// populate playlist.
// Get thumbnail value and assign to cellrenderer.
var thumb:String;
if (item.hasOwnProperty("@thumb") > 0)
{
thumb = item. @ thumb;
}// Send data to tileList.
tileList.addItem({label:item.attribute("desc").toXMLString(),
data:item.attribute("src").toXMLString(),
source:thumb});
}
// Select the first video.
tileList.selectedIndex = 0;
// Listen for item selection.
tileList.addEventListener(Event.CHANGE, listListener);
// And automatically load it into myVid.;
myVid.source = tileList.selectedItem.data;
// Pause video until selected or played.
//myVid.pause();
// Listen for end of current video, then play the next
myVid.addEventListener(VideoEvent.COMPLETE, vidComplete);
}
// Detect when new video is selected, and play it
function listListener(event:Event):void
{
myVid.play(event.target.selectedItem.data);
}
// listen for complete event; play next video
function vidComplete(eventObject:VideoEvent):void
{
if (tileList.selectedIndex < tileList.length - 1)
{
tileList.selectedIndex = tileList.selectedIndex + 1;
tileList.scrollToIndex(tileList.selectedIndex);
myVid.play(tileList.selectedItem.data);
}
-
It looks like you have it set up to play videos after it finishes loading using the vidComplete function. I'd suggest you only use this for autoplay (when the first vid has loaded). This is code off the top of my head without any testing:
Code:
function playVid(){
if(tileList.length>0){
tileList.selectedItem=randRange(0,tileList.length-1);
myVid.play(tileList.selectedItem.data);
}
}
There might be a bug but it should be something like this.
-
thanks for your help, i try to integrate this but without success, can you tell me how you would do this?
-
Sharing the flv would help. I'm not too familiar with xml so I need a point of reference to work with.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|