Search code examples
actionscript-3flashtimerstreamnetstream

TotalTime of a stream - Video Flash Player AS3


Goodmorning,

I'm working on a video flash player to stream. What I want to do, is to display the total time of the stream and not the time since the user is watching the stream. I have a problem now, is that when I pause then play the video, the current time restarts. Do you have any ideas to fix my problem and to solve the other one? :)

**I'm using NetStream


Solution

  • Alright, for the first problem, what you want to do is to setup a function that receives the MetaData of the video and save that value somewhere.

    First, when you create your NetStream Object, you need to add a Client to the NetStream that references the function onMetaData.

    var ns:NetStream;   //your NetStream Object
    var client:Object = new Object(); //Create an Object that represents the client
    client.onMetaData = onMetaData; //reference the function that catches the MetaData of the Video
    ns.client = client;             //assign our client Object to the client property of the NetStream
                                    //Once MetaData is available, it'll call onMetaData with all of the information
    
    function onMetaData(metaData:Object):void
    {
        duration = metaData.duration;   //duration is the variable that is supposed to total length of the video
    }
    

    Now with the duration value you get the total play time of the movie that is currently playing with that NetStream Object.


    You can solve your second problem in a number of ways, for example:

    • pause() and resume()
    • pause() and player('currentTime')

    Simply keep a Boolean Variable called pause that keeps track if the video is currently playing or not.

    var paused:Boolean = false;  //assuming the video is currently playing
    var currentTime:Number = 0;  
    var button:Button;    //some kind of play/pause button
    button.addEventListener(MouseEvent.CLICK,onButtonClick);
    
    function onButtonClick(event:MouseEvent):void
    {
        if(paused)
        {
            paused = false;
            ns.resume();
            //ns.play(currentTime)   //this also works
        }
        else
        {
            paused = true;
            ns.pause();
            currentTime = ns.time;
        }
    }