Home » Knowledge Base » flv-to-video.com / knowledge base
Detecting the end of & looping FLV files

Looping an *.flv video file requires being able to detect the end of the *.flv video, so this tutorial will cover both. The recommended method of detecting the end of the *.flv video playback is via the NetStream::onStatus event handler, by checking for NetStream.Play.Stop event. However, many people have reported that this event is not always sent (which could be due to undocumented bugs in the *.flv video implementation on the NetStream class). For that reason, we will also need to create workaround for *.flv video files that does not send the NetStream.Play.Stop event.

In this example, we use the NetConnection object to open an *.flv video file. The process of looping the movie is the similar when using FlashCom server. The process described here is the standard way of playing *.flv video files.

myNetconnection = new NetConnection();

myNetconnection.connect(null);

myStream = new NetStream(myNetconnection);

myStream.setBufferTime(3);

my_video.attachVideo(myStream);

myStream.play(videoUrl);

onStatus is a method of the NetStream class. To check for the end of *.flv video playback, we can override it. You can view the implementation of this method by examining the onStatus function on frame 1 of the main timeline of the sample movie.

myStream.onStatus = function(info)

{

trace(”info=”+info.code);

if (info.code == “NetStream.Play.Stop”)

{

// Set flag, we will need to wait for

// “NetStream.Buffer.Empty” before

// actually restarting the movie.

stopped=true;

}

if (info.code == “NetStream.Buffer.Empty”)

{

// At this point, the movie has stopped, and

// the buffer is empty.

// We’re ready to restart the movie.

if (stopped)

{

myStream.seek(0)

myStream.play(videoUrl);

stopped=false;

}

}

}

Here we need to check if the event being sent is NetStream.Play.Stop. When you receive NetStream.Play.Stop, it means that the movie has stopped playing. Note however, because *.flv video playback has a tendency to freeze when you start a movie without waiting for the playback buffer to be empty, we should wait for the NetStream.Buffer.Empty event as well. When both conditions are met, then we’re ready to replay the movie.

You can find more information about the onStatus event from Macromedia.

Beginning with *.flv video version 1.1, Macromedia has added a metadata section into a *.flv video file. The metadata contains the duration of the *.flv video, among other things. We can use the duration information to check whether a movie has reached the end of playback. Note that not all *.flv video files have metadata (you will need to use software that supports metadata to create the *.flv video).

We can retrieve the duration information by using the following code (frame 1 of the main timeline of the sample movie):

myStream.onMetaData = function(obj)

{

myStreamDuration=obj.duration;

trace(”metadata duration=”+myStreamDuration);

}

If the *.flv video has metadata, the code snippet above will show the duration of the *.flv video. You can use the Moyea FLV to SWF Converter to check for the presence of metadata – if the *.flv video has no metadata, Flix will show: “[*.flv video has no metadata]”.

If your *.flv video does not have metadata information, it is recommended that you re-encode the *.flv video with programs that supports metadata, such as Flix Pro or the Flix Exporter.

Checking for the end of the playback using metadata is quite straightforward. Just add a loop to check if the current playback time is equal to or greater than the duration. You can view the implementation of this method by examining the code on the frame 2 and 3 of the main timeline of the sample movie.

var maxIdle=5;

if (myStreamDuration>0)

{

myDelta=myStream.time-myStreamDuration;

if (myDelta<0)

myDelta=-myDelta;

if (myDelta<2)

{

if (myDelta==lastDelta)

{

noStreamMovement++;

}

else

{

lastDelta=myDelta;

noStreamMovement=0;

}

if (noStreamMovement>maxIdle)

{

trace(”loop”);

myStream.seek(0);

myStream.play(videoUrl);

lastDelta=0;

noStreamMovement=0;}

}

}

}