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

**CStrauss**- Graduate
- Posts: 122
- Loc: St. Louis MO. USA

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

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
- Posts: 23473
- Loc: Woodbridge VA

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/

http://schoolofflash.com/2008/04/10/tut ... nscript-3/

**CStrauss**- Graduate
- Posts: 122
- Loc: St. Louis MO. USA

**ATNO/TW**- Super Moderator
- Posts: 23473
- Loc: Woodbridge VA

**CStrauss**- Graduate
- Posts: 122
- Loc: St. Louis MO. USA

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

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;

}

if anyone can find a solution to my problems before I do and most my results please let me know.

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;

}

- 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;
- }

if anyone can find a solution to my problems before I do and most my results please let me know.

**ATNO/TW**- Super Moderator
- Posts: 23473
- Loc: Woodbridge VA

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:

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.

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);

}

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);

}

- 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);
- }

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
- Posts: 122
- Loc: St. Louis MO. USA

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.

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.

**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;

}

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;

}

- 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;
- }

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
- Posts: 23473
- Loc: Woodbridge VA

Page **1** of **1**

To Reply to this topic you need to LOGIN or REGISTER. It is free.

## 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