// you’re reading...

Programming tips

Flash Tip: Detecting end of videos

Concord Monitor's, Remember Me

“Remember Me” by Concord Monitor

I finally sat down today to watch “Remember Me,” a great multimedia project by Concord Monitor. Brent Foster of Visual Journalist posted the behind-the-scenes story, and I knew I wanted to spend quality time with it. As I ate lunch, I watched the presentation. The only problem was that nothing happened at the end of each of the six videos, disrupting my viewing experience as I had to manually start the next. For those who want to sit back and watch, it would have been nice to add a feature that detects the end of one video to immediately start the next. Here’s how to do it:

(For those who just want to dig in, download my files HERE.)

In Flash (AS2), give your video player the instance name video1. Attach a video to it and set the autoRewind to false (via Window –> Component Inspector). Then, add this code to the timeline.

var myListener = new Object();
myListener.complete = function(eventObject){
trace(“video1 is finished”);
video1.stop();
video1._visible = false;
video2._visible = true;
video2.play();
}
video1.addEventListener(“complete”, myListener);

This code adds a listener to the video to detect it’s completion, and will trigger the code within the function. For this example, we are tracing a message, stopping the first video, making it invisible, making the second video visible and playing it.

For the second video, give it the instance name “video2,” attach a video to it, set the autoPlay to false and the autoRewind to false. At the beginning of your code, add this line:

video2._visible = false;

This makes the second video player invisible at the beginning since we don’t want it showing. Then, add this code at the bottom:

var myListener2 = new Object();
myListener2.complete = function(eventObject){
trace(“video2 is finished”);
video2.stop();
video2._visible = false;
video1._visible = true;
video1.play();
}
video2.addEventListener(“complete”, myListener2);

This adds a second listener for the second video player, telling it to play the first video again when the second video is done. If you had multiple videos, just make sure to create a listener for each player, and turn on/off the visibility of each accordingly.

Other posts that might interest you:

Discussion

2 comments for “Flash Tip: Detecting end of videos”

  1. impressive … some people never share but I think you get back what you give …

    Thanks

    Posted by Benjamin | 22 November 2008, 8:22 AM
  2. Just found your site and really liking it.
    This piece of listener code is probably my most used and re-used. Very simple to implement but very powerful in terms of controlling what happens and when.

    Looking forward to reading more of your stuff….

    Posted by steve button | 18 September 2009, 4:04 AM

Post a comment

Go Daddy $6.99.com sale 125x125

Language

Subscribe to RSS Feed

English


Español

Archives