How Do I Display Duration and Total time in HH:MM:SS in AS3

Actionscript 3.0 questions.

How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby fluid5design » Tue Feb 19, 2008 4:38 pm

Hello all, I have been searching the internet for a long time with no success in order to figure out how to display the duration / Total Time of my flv in AS3.

I need this to work so that on my stage i can have a dynamic text box displaying the current time of the flv and the total time ---- example 3:45 / 6:58 where the 3:45 would update as the flv was scrubbed or played, or whenever a jump point was pressed. I found an example of this on this page. http://www.afcomponents.com/components/flv_player_as3/

I have been using the as3 code to import my video and use cue points and for the life of me I cant get this to work.

i have a video component on the stage and use import fl.video.FLVPlayback; to call in the video. I am not using netstream.

Please help!
Christopher Yin
Project Manager
Thomson Financial
http://www.fluid5design.com/digifolio.html
User avatar
fluid5design
 
Posts: 12
Joined: Tue Feb 19, 2008 4:16 pm

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby hasan » Thu Feb 21, 2008 3:44 am

Try this on for size:

Code: Select all
function formatTime(s:Number):String
{
   // Do some time calculations
   var hrs:Number = Math.floor(s/3600);
   var mins:Number = Math.floor((s%3600)/60);
   var secs:Number = Math.floor((s%3600)%60);
   
   // Define some vars
   var formattedTime:String = '';
   var hours:String = '';
   var minutes:String = '';
   var seconds:String = '';
   
   // Update the hours variable
   if (hrs != 0)
   {
      if (hrs < 10)
      {
         hours = '0' + hrs.toString() + ':';
      }
      else
      {
         hours = hrs.toString() + ':';
      }
   }
   
   // Update the minutes variable
   if (mins < 10)
   {
      minutes = '0' + mins.toString() + ':';
   }
   else
   {
      minutes = mins.toString() + ':';
   }
   
   // Update the seconds variable
   if (secs < 10)
   {
      seconds = '0' + secs.toString();
   }
   else
   {
      seconds = secs.toString();
   }
   
   // Update the formattedTime variable;
   formattedTime = hours + minutes + seconds;
   
   // Kick the formatted time back out
   return formattedTime;
}


where s = NetStream.time. HTH...:)
User avatar
hasan
 
Posts: 936
Joined: Mon Mar 13, 2006 12:37 pm

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby Pixelmixer » Thu Feb 21, 2008 5:35 am

flvplayback.totalTime = total video length
flvplayback.playheadTime = current time

Youll have to do some math ( t/60 ... t/100 ... etc) and string formatting like hasans example to get your ideal format.

Hth
Dustin J Sparks
Interactive Developer/Student
Muncie, Indiana
http://djsparks.iweb.bsu.edu/portfolio
(Adobe Certified Flash CS3 Expert, AS3 Expert, Adobe Certified Dreamweaver CS3 Expert)
User avatar
Pixelmixer
 
Posts: 308
Joined: Mon Mar 13, 2006 2:23 pm
Location: Muncie, Indiana

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby fluid5design » Thu Feb 21, 2008 5:04 pm

Thanks for responding so quickly. I have to say I feel pretty stupid when it comes to AS3. but here is my code, how do i implement the code you gave me to my code. And how do i link the results to a dynamic txt box on my stage?


Code: Select all

import fl.video.*;
            
var flvControl = display;
var flvSource = "video.flv";

flvControl.source = flvSource;
flvControl.align = VideoAlign.CENTER;
flvControl.scaleMode = VideoScaleMode.MAINTAIN_ASPECT_RATIO;

// Add seek functionality to the navigation buttons
// Start C1 Nav
function seekHandler_C1(event:MouseEvent):void
{
   flvControl.seekToNavCuePoint("C1")
}

C1_btn.addEventListener(MouseEvent.CLICK, seekHandler_C1);
// End C1 Nav

// Start C2 Nav
function seekHandler_C2(event:MouseEvent):void
{
   flvControl.seekToNavCuePoint("C2")
}

C2_btn.addEventListener(MouseEvent.CLICK, seekHandler_C2);
//End C2 Nav

// Start C3 Nav
function seekHandler_C3(event:MouseEvent):void
{
   flvControl.seekToNavCuePoint("C3")
}

C3_btn.addEventListener(MouseEvent.CLICK, seekHandler_C3);
//End C3 Nav

// Start C4 Nav
function seekHandler_C4(event:MouseEvent):void
{
   flvControl.seekToNavCuePoint("C4")
//End C4 Nav




Also.. I am a flash designer who is now aspiring to be an ActionScript programmer. I, until AS3, have been able to understand how to make snippets of code work together in AS1 and AS2. But now with AS3 and OOP, I want to understand fully how it all works together, can you guys recommend some avenues of study so that I may learn AS3 easily, classes, books, etc. I have researched so many things and its been very confusing as it seems that there are many different ways to get the same thing done depending what author you read.

Again, thanks to you all for the response. If you have any questions with bringing HD video to FLV workflows etc let me know.

Chris
Last edited by fluid5design on Thu Feb 21, 2008 5:17 pm, edited 1 time in total.
Christopher Yin
Project Manager
Thomson Financial
http://www.fluid5design.com/digifolio.html
User avatar
fluid5design
 
Posts: 12
Joined: Tue Feb 19, 2008 4:16 pm

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby Pixelmixer » Thu Feb 21, 2008 5:11 pm

This code might help you out. It's a project i worked on last summer. Its unfinished, but the video part works. I commented it quite a bit so maybe itll help. Just pay attention to the flv portion.

http://code.google.com/p/synthea/source ... oObject.as
Dustin J Sparks
Interactive Developer/Student
Muncie, Indiana
http://djsparks.iweb.bsu.edu/portfolio
(Adobe Certified Flash CS3 Expert, AS3 Expert, Adobe Certified Dreamweaver CS3 Expert)
User avatar
Pixelmixer
 
Posts: 308
Joined: Mon Mar 13, 2006 2:23 pm
Location: Muncie, Indiana

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby fluid5design » Thu Feb 21, 2008 5:21 pm

Wow, thanks for the quick response, I will check it out and see what i can do to get this to work. Again thanks for lookin out!
Christopher Yin
Project Manager
Thomson Financial
http://www.fluid5design.com/digifolio.html
User avatar
fluid5design
 
Posts: 12
Joined: Tue Feb 19, 2008 4:16 pm

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby reyco1 » Mon Feb 25, 2008 2:15 am

or you can use this function which will return seconds in a HH:MM:SS format:

Code: Select all
function secToFormattedTime($seconds:Number):String
{
   var input:Number = $seconds;
   var time:String = (input > 3600 ? Math.floor(input / 3600) + ":" : "") + (input % 3600 < 600 ? "0" : "") + Math.floor(input%3600/60) + ":" + (input%60 < 10 ? "0":"") + input % 60;
   return time;
}
reyco1
 
Posts: 225
Joined: Thu Mar 16, 2006 10:57 am
Location: New York

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby fluid5design » Thu Feb 28, 2008 9:19 pm

Got it to work!


Use this, though I cant get rid of the hundredths of seconds, would love to know how! I left the tracing on so you can see it work.
Code: Select all

// Elapsed and Total Time
import fl.video.*;

video.addEventListener(MetadataEvent.METADATA_RECEIVED, timeListener);

function timeListener(eventObject:MetadataEvent):void {
   var totalSeconds = String(eventObject.info.duration);
   var durationTime:String = (totalSeconds > 3600 ? Math.floor(totalSeconds / 3600) + ":" : "") + (totalSeconds % 3600 < 600 ? "0" : "") + Math.floor(totalSeconds % 3600/60) + ":" + (totalSeconds % 60 < 10 ? "0":"") + totalSeconds % 60;
   totalTime.text = durationTime;
   trace("Total time is: " + eventObject.info.duration);
}

stage.addEventListener(Event.ENTER_FRAME, updateTime);

function updateTime (ev:Event):void {
   var elapsedSeconds = String(video.playheadTime);
   var runTime:String = (elapsedSeconds > 3600 ? Math.floor(elapsedSeconds / 3600) + ":" : "") + (elapsedSeconds % 3600 < 600 ? "0" : "") + Math.floor(elapsedSeconds % 3600/60) + ":" + (elapsedSeconds % 60 < 10 ? "0":"") + elapsedSeconds % 60 ;
   elapsedTime.text = runTime;
   trace("Elapsed time in minutes:seconds: " + runTime);
}
Christopher Yin
Project Manager
Thomson Financial
http://www.fluid5design.com/digifolio.html
User avatar
fluid5design
 
Posts: 12
Joined: Tue Feb 19, 2008 4:16 pm

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby flaflv » Fri Jun 27, 2008 7:47 pm

Thanks for sharing the code. Any clue on how to get rid of that hundredth of a sec?
flaflv
 
Posts: 6
Joined: Thu Jun 19, 2008 8:50 pm
Flash Version: Adobe Flash CS5

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby flaflv » Tue Jul 01, 2008 2:52 pm

Anyone please help???
flaflv
 
Posts: 6
Joined: Thu Jun 19, 2008 8:50 pm
Flash Version: Adobe Flash CS5

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby James » Tue Jul 01, 2008 7:41 pm

Don't be put off by the fancy evaluation using the ?: statement (it's basically just adding a zero if the value is less than ten so that you get 01:09 instead of 1:9). You just have to floor the final value:
Code: Select all
// Elapsed and Total Time
import fl.video.*;

video.addEventListener(MetadataEvent.METADATA_RECEIVED, timeListener);

function timeListener(eventObject:MetadataEvent):void {
   var totalSeconds = String(eventObject.info.duration);
   var durationTime:String = (totalSeconds > 3600 ? Math.floor(totalSeconds / 3600) + ":" : "") + (totalSeconds % 3600 < 600 ? "0" : "") + Math.floor(totalSeconds % 3600/60) + ":" + (totalSeconds % 60 < 10 ? "0":"") + Math.floor(totalSeconds % 60);
   totalTime.text = durationTime;
   trace("Total time is: " + eventObject.info.duration);
}

stage.addEventListener(Event.ENTER_FRAME, updateTime);

function updateTime (ev:Event):void {
   var elapsedSeconds = String(video.playheadTime*10);
   var runTime:String = (elapsedSeconds > 3600 ? Math.floor(elapsedSeconds / 3600) + ":" : "") + (elapsedSeconds % 3600 < 600 ? "0" : "") + Math.floor(elapsedSeconds % 3600/60) + ":" + (elapsedSeconds % 60 < 10 ? "0":"") + Math.floor(elapsedSeconds % 60) ;
   elapsedTime.text = runTime;
   trace("Elapsed time in minutes:seconds: " + runTime);
}
Image
Image
Please consider donating to GTAL if you can. Click donate at the top-right of this page.
User avatar
James
Moderator
 
Posts: 406
Joined: Sat Oct 13, 2007 9:29 pm
Location: London, UK
Flash Version: Adobe Flash CS5.5
Authoring Environment: Apple OS

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby James » Tue Jul 01, 2008 7:57 pm

Just to explain the ? and : symbols. This is a cool sort of almost if/else statement:

expression1 ? expression2 : expression3

If expression one evaluates to true, then expression2 is returned, if expression1 evaluates to false then expression3 is returned. So:

Code: Select all
var myNumber:int = 15;
trace(myNumber > 15 ? "This is true!" : "This is false"); //Output: "This is false".
trace(myNumber == 15 ? "This is true!" : "This is false"); //Output: "This is true!".


Hope that comes in handy for those of you who didn't know about it. Displaying the time is a great use for it, but this is all I have ever used it for.
Image
Image
Please consider donating to GTAL if you can. Click donate at the top-right of this page.
User avatar
James
Moderator
 
Posts: 406
Joined: Sat Oct 13, 2007 9:29 pm
Location: London, UK
Flash Version: Adobe Flash CS5.5
Authoring Environment: Apple OS

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby turbowebguy » Wed Oct 08, 2008 11:06 pm

I quick solution (which I'm always in search for) I found to getting the seconds down:

function updateTime (ev:Event):void {
var elapsedSeconds = String(video.playheadTime*10/5/2);

var runTime:String = (elapsedSeconds > 3600 ? Math.floor(elapsedSeconds / 3600) + ":" : "") + (elapsedSeconds % 3600 < 600 ? "0" : "") + Math.floor(elapsedSeconds % 3600/60) + ":" + (elapsedSeconds % 60 < 10 ? "0":"") + Math.floor(elapsedSeconds % 60) ;
elapsedTime.text = runTime;
trace("Elapsed time in minutes:seconds: " + runTime);
}


I'm sure there's a better way to do it but this worked for me (for now) in AS3!

-Aaron, e.g. turbowebguy dot com
"Patience is a virtue for those who have the time."
User avatar
turbowebguy
 
Posts: 9
Joined: Wed Oct 08, 2008 10:47 pm
Location: Sunny Southern California, USA
Flash Version: Adobe Flash CS5
Authoring Environment: Microsoft Windows

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby G.Bob » Tue Jan 06, 2009 4:48 am

You all just saved me a lot of time. Thanks.
G.Bob
 
Posts: 1
Joined: Tue Jan 06, 2009 4:46 am
Flash Version: Adobe Flash CS5.5

Re: How Do I Display Duration and Total time in HH:MM:SS in AS3

Postby joejubee » Wed Jan 07, 2009 2:01 pm

I second that. Thank you very much for this info. You saved me a bunch of time and headaches with this.

Joe.

Need a killer website, or e-learning program? Visit http://www.joeFREELANCE.com.

I'm listening to The Classic Metal Show http://www.cmsradionetwork.com right now.


Image
User avatar
joejubee
 
Posts: 1
Joined: Wed Jan 07, 2009 1:54 pm
Flash Version: Adobe Flash CS5

Next

Return to Actionscript 3.0

Who is online

Users browsing this forum: No registered users and 1 guest

cron