FLVPlayback组件和Actionscript。使用数组进行随机Flv播放。

时间:2011-08-16 14:21:32

标签: actionscript-3

我的项目越来越近了,还需要一些帮助。我有5个Flvs,我想在这个网页上随机播放www.music-puppets.com /

我创建的.fla文件包含以下代码:

var files:Array = [ "Sz01Puppet.flv", "Sz02Puppet.flv", "Sz03Puppet.flv", "Sz04Puppet.flv", "Sz05Puppet.flv" ];
var shuffledFiles:Array = shuffleArray(files);
//quick test
var testTimer:Timer = new Timer(1000);
testTimer.addEventListener(TimerEvent.TIMER,updateFile);
testTimer.start();

function updateFile(event:TimerEvent):void{
if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files); //all files played, repeat process
trace('play file',shuffledFiles[0]);
shuffledFiles.shift();
}
function shuffleArray(source:Array,clone:Boolean = true):Array {
var output:Array = [];
var input:Array = clone ? [].concat(source) : source; //clone ? preserve orignal items by making a copy for shuffling, or not
while(input.length) output.push(input.splice(int(Math.random() * input.length-1),1) [0]);
return output;

}

此脚本有效。在输出中,每个flv随机列出,然后重复。接下来我希望这个AS脚本能够与FLV组件一起使用。

但是如何让它工作?

在我的库中,我有5个flv和flvplayback组件。 我将FLVPlayback组件拖到舞台上,但我只能在源代码中添加一个flv。如何让我的工作动作脚本与FLVPlayback组件一起使用。

在这里你可以看到我的屏幕是怎样的。

capture01.jpg

capture02.jpg

很高兴得到一些反馈:)

2 个答案:

答案 0 :(得分:0)

首先,您需要使用属性面板为FLVPlayback组件提供实例名称。这将让你从代码中与它交谈。

接下来,您需要向FLVPlayback组件添加一个侦听器,以检测每个视频的播放时间。我们假设你给它一个实例名称myVideo

myVideo.addEventListener(VideoEvent.COMPLETE,onVideoComplete);

你需要一个处理函数,它看起来类似于你目前用于计时器每次迭代的函数:

function onVideoComplete(event:VideoEvent):void {
    if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files); //all files played, repeat process
    myVideo.source = shuffledFiles.shift(); //grab the first array item to play in the FLVPlayback component
}

最后,请确保您的FLVPlayback组件设置为自动播放新来源,然后分配第一个视频以实现目标:

myVideo.autoPlay = true;
myVideo.source = shuffledFiles.shift();

答案 1 :(得分:0)

如果有人有兴趣。我有运行代码:)

import flash.events.Event;
import fl.video.*;

var files:Array;
var shuffledFiles:Array;

loaderInfo.addEventListener(Event.COMPLETE,ready);
function ready(event:Event):void{
    loaderInfo.removeEventListener(Event.COMPLETE,ready);
    //swf rescale setup
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.addEventListener(Event.RESIZE,stageResized);
    //get FlashVars - a string converted into an Array by spliting it on the , character
    //if the files FlashVar is setup correctly use the data, else use default values
    if(loaderInfo.parameters.files != undefined) files = loaderInfo.parameters.files.indexOf(',') > 0 ? loaderInfo.parameters.files.split(",") : [loaderInfo.parameters.files];
    else files = [ "Sz01Puppet.flv", "Sz02Puppet.flv", "Sz03Puppet.flv", "Sz04Puppet.flv", "Sz05Puppet.flv" ];
    shuffledFiles = shuffleArray(files);
    //play the 1st video
    videoPlayer.source = shuffledFiles[0];
    shuffledFiles.shift();

    //see when the video finished playing
    videoPlayer.addEventListener(VideoEvent.COMPLETE,videoFinished);
}
function videoFinished(event:VideoEvent):void{
    if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files);//all files played, repeat process
    videoPlayer.source = shuffledFiles[0];//play the first video in the random list
    videoPlayer.play();
    trace('playing',shuffledFiles[0]);

    shuffledFiles.shift();//remove the first video from the random list (e.g. [2,0,1].shift() becomes [0,1])
}
function stageResized(event:Event):void{
    videoPlayer.width = stage.stageWidth;
    videoPlayer.height = stage.stageHeight;
}
function shuffleArray(source:Array,clone:Boolean = true):Array {
    var output:Array = [];
    var input:Array = clone ? [].concat(source) : source;//clone ? preserve orignal items by making a copy for shuffling, or not
    while(input.length) output.push(input.splice(int(Math.random() * input.length-1),1)[0]);
    return output;
}