1

我正在尝试创建一个从 AS3 中的 XML 播放列表中调用随机视频的函数,但不知道从哪里开始。我已经尝试了多种方法并且正在打破我的大脑。请停下来!

虽然我有一些运气拉视频,但随机化不起作用,我还需要从 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();
}
4

1 回答 1

1

您可以像这样访问随机播放列表节点(假设您的 XML 由名为“xml”的变量引用):

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

您可以像这样访问属性:

var randomURL:String = randomVideo.@url;
var randomTitle:String = randomVideo.@title;
于 2013-04-27T02:47:14.100 回答