How to use the length of a flv to display a countdown clock

  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

OK here is my situation. I'm working on a flash project that is basically your typical score board you see at a football game. In the middle is a flv that is playing. I know the time of the video is 1 min 50.254 seconds. I have a clock on my scoreboard and I want it to count down using the length of the flv file down to 0:00, like a game clock would.

Is there a way I can use the playHeadTime method to do that? if anyone has done something like this can they please post a sample piece of code for me to look to see how it was done?

Im looking for something using Actionscript 3

Thanks
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23458
  • Loc: Woodbridge VA

Post 3+ Months Ago

I've never done it, but since you know the length of the FLV I don't think you have to actually use the playhead time. Assuming time is consistent (given playback speed, download, preload, buffer, etc) you should be able to use something like this for your scoreboard

http://schoolofflash.com/2008/04/10/tut ... nscript-3/
  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

yeah i saw that tutorial and have it as a back up option , it just seems like a lot of unnecessary code to type using that tutorial if i have access to the play head time and wanting to know if there is a some math functions to display it and just have it count backwards
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23458
  • Loc: Woodbridge VA

Post 3+ Months Ago

This might help
http://www.actionscript.org/forums/arch ... 45812.html
  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

OK, I figured something out by looking at some code that displays the time played / total time just trying to reverse it. I got it sort of to work there are some problems so I will post the link to see it and the code I used to see if anyone can help me figure out the time subtracting things.

One of the things I'm trying to achieve is to have leading 0's before the minutes and seconds when needed. but here is my link and code see for yourself just watch the clock and you see the problems I'm having.

http://stcharlestitans.com

Code: [ Select ]
 
this.addEventListener(Event.ENTER_FRAME, updateNP);
 
function updateNP(event:Event):void {
    var totalSeconds:Number = flvControl.playheadTime;
    var totalSeconds2:Number =  flvControl.totalTime; // total time in miliseconds
    var minutes:Number = Math.floor(totalSeconds /60);
    var minutes2:Number = Math.floor(totalSeconds2 /60); // total minutes
    var seconds = Math.floor (totalSeconds) % 60;
    var seconds2 = Math.floor (totalSeconds2) % 60; // total seconds
    //trace(minutes2 +":"+seconds2);
   
    var currentMinute = minutes2 - minutes;
    var currentSeconds = seconds2 - seconds;
 
    //trace(currentMinute+":"+currentSeconds);
    timeBox_txt.text = "0" + currentMinute + ":" + currentSeconds;
 
}
 
  1.  
  2. this.addEventListener(Event.ENTER_FRAME, updateNP);
  3.  
  4. function updateNP(event:Event):void {
  5.     var totalSeconds:Number = flvControl.playheadTime;
  6.     var totalSeconds2:Number =  flvControl.totalTime; // total time in miliseconds
  7.     var minutes:Number = Math.floor(totalSeconds /60);
  8.     var minutes2:Number = Math.floor(totalSeconds2 /60); // total minutes
  9.     var seconds = Math.floor (totalSeconds) % 60;
  10.     var seconds2 = Math.floor (totalSeconds2) % 60; // total seconds
  11.     //trace(minutes2 +":"+seconds2);
  12.    
  13.     var currentMinute = minutes2 - minutes;
  14.     var currentSeconds = seconds2 - seconds;
  15.  
  16.     //trace(currentMinute+":"+currentSeconds);
  17.     timeBox_txt.text = "0" + currentMinute + ":" + currentSeconds;
  18.  
  19. }
  20.  


if anyone can find a solution to my problems before I do and most my results please let me know.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23458
  • Loc: Woodbridge VA

Post 3+ Months Ago

I messed around with this this morning and came up with something that works. It isn't the prettiest code in the world and there may be an easier way to do it but it works so what the heck. My video is 3:32. Here's the working example that does exactly what you're after (even works perfectly if you scrub the playhead):

http://www.alaron-nuclear.com/devsite/f ... vtime.html

Here's the code I used:
Code: [ Select ]
this.addEventListener(Event.ENTER_FRAME, updateNP);
 
function updateNP(event:Event):void {
    var totalSeconds:Number = flvControl.playheadTime;
    var totalSeconds2:Number =  flvControl.totalTime; // total time in miliseconds
    var minutes:Number = Math.floor(totalSeconds /60);
    var minutes2:Number = Math.floor(totalSeconds2 /60); // total minutes
    var seconds = Math.floor ((totalSeconds) % 60)-27; // this keeps the clock from going into  negative numbers - it's the difference between the total starting seconds and 59 seconds. In your case your clock starts at 50 so this should be 9 for you
    var seconds2 = Math.floor (totalSeconds2) % 60; // total seconds
    var currentMinute = minutes2 - minutes;
    var currentSeconds = seconds2 - seconds;
   
    //display the clock
    //currentSeconds starts at 60 as you can tell by the trace - in my movie if I go below 27 it counts into negative numbers and this also compensates for the change in minutes. The minute won't change until currentSeconds reaches 0 so we compensate for that here
    if (currentSeconds < 27) {
    timeBox_txt.text = "0" + (currentMinute - 1) + ":" + (currentSeconds + 32);
    }
    //this displays the countdown time normally as long as the display is greater than 9
    else if (currentSeconds > 36   ) {
        timeBox_txt.text = "0" + currentMinute + ":" + (currentSeconds - 27);
    }
    //if the display is 9-0 this adds the leading 0 to the seconds.
    else if ((currentSeconds < 36) + (currentSeconds > 27)) {
        timeBox_txt.text = "0" + currentMinute + ":0" + (currentSeconds - 27);
    }
    //trace(currentSeconds);
     }
 
  1. this.addEventListener(Event.ENTER_FRAME, updateNP);
  2.  
  3. function updateNP(event:Event):void {
  4.     var totalSeconds:Number = flvControl.playheadTime;
  5.     var totalSeconds2:Number =  flvControl.totalTime; // total time in miliseconds
  6.     var minutes:Number = Math.floor(totalSeconds /60);
  7.     var minutes2:Number = Math.floor(totalSeconds2 /60); // total minutes
  8.     var seconds = Math.floor ((totalSeconds) % 60)-27; // this keeps the clock from going into  negative numbers - it's the difference between the total starting seconds and 59 seconds. In your case your clock starts at 50 so this should be 9 for you
  9.     var seconds2 = Math.floor (totalSeconds2) % 60; // total seconds
  10.     var currentMinute = minutes2 - minutes;
  11.     var currentSeconds = seconds2 - seconds;
  12.    
  13.     //display the clock
  14.     //currentSeconds starts at 60 as you can tell by the trace - in my movie if I go below 27 it counts into negative numbers and this also compensates for the change in minutes. The minute won't change until currentSeconds reaches 0 so we compensate for that here
  15.     if (currentSeconds < 27) {
  16.     timeBox_txt.text = "0" + (currentMinute - 1) + ":" + (currentSeconds + 32);
  17.     }
  18.     //this displays the countdown time normally as long as the display is greater than 9
  19.     else if (currentSeconds > 36   ) {
  20.         timeBox_txt.text = "0" + currentMinute + ":" + (currentSeconds - 27);
  21.     }
  22.     //if the display is 9-0 this adds the leading 0 to the seconds.
  23.     else if ((currentSeconds < 36) + (currentSeconds > 27)) {
  24.         timeBox_txt.text = "0" + currentMinute + ":0" + (currentSeconds - 27);
  25.     }
  26.     //trace(currentSeconds);
  27.      }
  28.  


In your case your clock starts at 50 seconds, so just adjust the numbers accordingly. Like I said, I pretty much did that by trial and error. There very well could be a simpler way to execute that. If anyone can figure out an easier way, I'd love to know, because I will use this in the future with a majority of my FLV's. Also if your FLV is a large file, I'd recommend using a preloader so the clock doesn't display before your FLV does.
  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

Thanks for taking the time ATNO/TW to write that code. After some trial and error myself this morning and searching through the help docs I did find a shorter way to do it there is still some issues I will discuss in a min but here is the code I did to achieve my counter backwards. It is a much shorter way to do it.

Code: [ Select ]
this.addEventListener(Event.ENTER_FRAME, updateNP);

function updateNP(event:Event):void {
    var totalSeconds:Number = flvControl.playheadTime; // current time on playhead
    var totalSeconds2:Number = flvControl.totalTime; // total time in miliseconds
    
    var timeLeft:Number = totalSeconds2 - totalSeconds;
    var minutes:Number = Math.floor(timeLeft / 60);
    var seconds:Number = Math.floor(timeLeft % 60);
    var min:String = minutes.toString();
    var sec:String = seconds.toString();
    
    if(minutes < 10){
        min = "0" + min;
    }
    if(seconds < 10){
        sec = "0" + sec;
    }
    
    //trace(min+":"+sec);
    timeBox_txt.text = min + ":" + sec;
}
  1. this.addEventListener(Event.ENTER_FRAME, updateNP);
  2. function updateNP(event:Event):void {
  3.     var totalSeconds:Number = flvControl.playheadTime; // current time on playhead
  4.     var totalSeconds2:Number = flvControl.totalTime; // total time in miliseconds
  5.     
  6.     var timeLeft:Number = totalSeconds2 - totalSeconds;
  7.     var minutes:Number = Math.floor(timeLeft / 60);
  8.     var seconds:Number = Math.floor(timeLeft % 60);
  9.     var min:String = minutes.toString();
  10.     var sec:String = seconds.toString();
  11.     
  12.     if(minutes < 10){
  13.         min = "0" + min;
  14.     }
  15.     if(seconds < 10){
  16.         sec = "0" + sec;
  17.     }
  18.     
  19.     //trace(min+":"+sec);
  20.     timeBox_txt.text = min + ":" + sec;
  21. }


Basically what I did is take the playheadTime which would be the current time in the flv file and subtracted it from the totalTime which gave me the time in milliseconds.

Then I did some conversions using the Math.floor to get them into manageable numbers.

Now this next part is how I dealt with the - numbers and adding leading 0's. It took me a bit cause the coding is different in AS3 then what it was in AS2, which is IMO not a very good move by adobe to not make AS3 very backward compatible with AS2 script but that is another topic.

But what I did was convert my numbers to string to display in the Game clock checking if the value while it still a number is less then 10 and if so then add a 0 to the string. then from there display it in the dynamic text field.

The only issue I really have to deal with is when the file begins to play for quick flash in the game clock you see the NaN for not a number flash before it changes to the time. So I'm currently trying to figure out how to get a preloader to work hoping that fixes it if not I can probably add another if statement to say something if the value is NaN display 1:50 then it will change once the script gets going.

But like you this might not be the most professional way, like I said it just took some fiddling with my variables and converting them to string and what not to get it to work but it is a much condensed version of what you did. But none the less at the very lest we found two ways for others to look at and build off of.

Again thanks for the time you spent playing around with my code.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23458
  • Loc: Woodbridge VA

Post 3+ Months Ago

Yeah, I got the NaN flash multiple times while I was fiddling. my final code eliminated that but have no clue why. Think I'll attempt your version tomorrow.
  • CStrauss
  • Graduate
  • Graduate
  • User avatar
  • Posts: 122
  • Loc: St. Louis MO. USA

Post 3+ Months Ago

good deal if you have any problems with it let me know

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.