Quick Tip: Consecutive playing flv files

I stopped counting how many times I’ve seen a post entry on the adobe / pixel2life / ultrashock forums about how to consecutive play flv files using an flvplayback component.

Actually it’s quite easy, and it just needs a few lines of code.

To start off drag and drop an flv playback component to your stage and give it the instancename “videoPlayer”.
Then as usual lock that layer, create a new layer called actions and lock that layer as well so you can’t add any objects to it.
Press the F9 key to bring up the actions panel and start typing :)

First we need to import the video package that is already included in the out-of-the box installation of Flash CS3.
Whyever, if you don’t use this single import line the videoEvent that we are using is not being recognized and will throw an error.

1
import fl.video.*;

Next up we create a simple array holding two flv videos called videoArray and define variable “i” as a Number starting with 0. We are going to use “i” as a “controller to our array.

1
2
var videoArray:Array = new Array("test1.flv", "test2.flv");
var i:Number = 0;

Then our mainfunction called init() has the wonderful job of adding the current array object “0″ as a source to our FLV Playback component and the most important add an EventListener to the FLVPlayback component that listens for a completion of the FLV. As soon as the FLV has completely played the function nextVideo() is called up.

1
2
3
4
function init():void{
	videoPlayer.source = videoArray [i];
	videoPlayer.addEventListener(VideoEvent.COMPLETE, nextVideo);
}

Which just increments our i by 1 and tells our FLVPlayback componen to play the next array object.

1
2
3
4
function nextVideo(e:VideoEvent){
	i++;
	videoPlayer.play(videoArray [i]);
}

Last but not least you need to initialize your function init() :)

1
init();

As you see not hard at all, keep in mind that this only works for the FLVPlayback component!!

Next time I will show you how to do the same but using the lightweight videoobject instead of the heavy FLVPlayback component.

Keep it up :)
Tiago

Leave a comment

13 Comments.

  1. you could control in the nextvideo function if you got to the end of the array and set i=0

    Great tutorial Tiago!

  2. But how would you do it if you needed to use an xml file that contained all the FLV locations, XML captions files, preview image, etc?

    Thanks,

    Joe

  3. Great tutorial, thank you very much.

    I only have one question. How should one go to avoid the second video from repeating itself upon completion?

    That’s happening to me and I’m using Flash CS4 with AS 3.0

    Thanks in advance.

    Moriz

  4. Great tutorial, thank you very much.

  5. Thanks for this good post…

  6. Great tutorial, thank

  7. Great Tutorial!
    I have the same question as Moris. what should i use to stop it from looping? I have 3 videos that i want to play and then stop. Any help would be great

  8. Basically you just need to create a simple if statement inside the next video method:

    function nextVideo(e:VideoEvent):void
    {
    if ( i == videoArray.length )
    {
    // do something else
    }
    else
    {
    i++;
    videoPlayer.play(videoArray [i]);
    }
    }

  9. Great tutorial. better than my friend teach me ha ha. thank a lot

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">