Friday, November 16, 2012

how to know the end of the file in action script!!!


There are two ways to find the end of the file using action script for the flv files.
  1. Using meta data (which we will get by getting meta event onMetaData)
  2. Using netStatus code "NetStream.Play.Stop"
Using onMetaData: When this event occurs, we need to take the duration of the file by using mdata.duration, which gives the duration of the file. After getting the duration of the file, we need to compare with the buffer length of the file while playing. Actually playing flv file in action script is, taking the fixed buffer length and play that buffer and after completing that buffer again take buffer and play that buffer, this process will continue till it reaches empty. So if the file is empty, buffer length reaches duration of the file or more. For us this condition is important, for checking end of the file, we can check this condition as shown in the sample code below.
 

Getting duration of the file:
public function onMetaEvent(mdata:Object):void
{
      //taking the duraiton time for looping
      lastPosition = mdata.duration;
}

Finding the end of the file:
public function netStatus(item:Object):void
{
   //Unpause.Notify code will be generated whenever buffer fills with data
   if (item.info.code == "NetStream.Unpause.Notify")
   {
      if (Math.floor(stream.bufferLength) >= Math.floor(lastPosition))
      {
         //buffer length reaches end of the file
      }
   }
}

Using netStatus code "NetStream.Play.Stop" : Here we need to check for the NetStream status code Play.Stop. as shown in the below sample code.

public function netStatus(item:Object):void
{
   if (item.info.code == "NetStream.Play.Stop")
   {
      //reaches end of the file
   }

}

For more NetStream status codes click here.

No comments:

Popular Posts