I’ve written a Timecode class that will take the ns.time property and return a timecode string in the “00:00″ format. See below for how it is used.
[as]var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
thevideo.attachVideo(ns);
ns.play(“yourFLV.flv”);
var tc:Timecode = new Timecode(ns);
this.onEnterFrame = function() {
trace(tc.getTimecode());
}[/as]
There is also a “setDelimiter()” method available if you want change the colon to some other character.
tc.setDelimiter(“|”);
You can download the class here.
Lee








This is all great stuff. Keep them coming.
Love the site music.
Hey Lee,
I went ahead and downloaded the Timecome class that I placed with my SWF. Once I test my movie, an action script windows pops up showing the the the class is working, though on the movie itself, no timer is showing.
I guess I have to create an instance or a variable to store the content of the class and displays it as the movie goes…I’m not sure?
hi, Lee.. thanks for your tutorials.. they are helping me a lot
i have one ask… how i associate the new class you made in my fla?
hope you can help me one more time
thanks
i placed lee’s Timecode.as in my classes folder and then used it in the following way:
set up you flv as usual (net connection and net stream) create a dynamic text field on the stage and name it timecode_txt (in this case) add the following actionscript to the rest of your video actionscript and test the movie. Make sure you FLVs have MetaData.
var tc:Timecode = new Timecode(ns);
timecode_txt.autoSize = true;
timecode_txt.selectable = false;
this.onEnterFrame = function() {
timecode_txt.text = tc.getTimecode();
}
Cheers Lee – this has really helped.
Andy
Lee — I’m new to Flash. Your Timecode Class and Andy’s code has really helped. My question is: how to I get the timer to restart when I play a new video?
How can I show the total running time of my FLV file?
For example if my clip is 5 minutes and 20 seconds I want to have the timecode above running and have next to it:
/ 05:20
This works great…many thanks…Lee rules!!!
Does any1 know how to implement a total time with this like YouTube?…
00:00 / 03:23
Hi,
I can not get this to work, I have tried Andy’s instructions that seem clear but I don’t understand where to store the class.
NEVERMIND, got it
import timecode.as
I didn’t realize it was that easy, thank you.
Hey guys, I’m very new to flash and had a heck of a time trying to figure out how to add a timecode to a video instance without net streaming, I’m Creating a flash application to run off of a cd and wanted all of my videos to load into the same flvplayback instance. Well it took a day of help searching browbeating and picking apart lee’s .as file but here is what I Came up with, By using a Timer Event
…var VideoTimer:Timer = new Timer(1000, 600);
…VideoTimer.addEventListener(TimerEvent.TIMER, AddSec);
and the playheadTime feature
…var dacount:uint = ThePlayback.playheadTime;
I was able to create a Dynamic Text field that updated every second by putting most of lee’s code into the AddSec Function,
…function AddSec(e:TimerEvent) {
dacount = ThePlayback.playheadTime;
var t:Number = Math.round(dacount);
var min:Number = Math.floor(t/60);
var sec:Number = t%60;
var tc:String = new String(“”);
if (min = 1) {
tc += min.toString();
} else {
tc += “0″;
}
tc += this.seprator;
if (sec < 10) {
tc += “0″;
tc += sec.toString();
} else {
tc += sec.toString();
}
TheTimer.text = tc;
…}
with this I could start and stop the timer using
…VideoTimer.start();
and
…VideoTimer.stop();
That i controlled with video events like
ThePlayback.addEventListener(VideoEvent.STOPPED_STATE_ENTERED,CloseIt);
ThePlayback.addEventListener(VideoEvent.PLAYING_STATE_ENTERED,OpenIt);
Obviouse Things to note, my FLVPlayback instance was “ThePlayback”
and my dynamic text instance was “TheTimer”
Also this only works on videos up to 10 minutes, where you define the VideoTimer Variable the last number is the total amount of seconds for the timer so for longer videos you can just up the timer.
Thanks for helping me out guys I hope this can help some more actionscript newbies in the future.
Hello,
I use flash rarely (self taught) and quite get this code to work for me. Can anyone suggest
what to do? I’ve created a dynamic text and called it timecode_txt as well. This is what I have currently for my action script (which works). Thank you for your help in advance.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
theVideo.attachVideo(ns);
ns.play(“http://futureperfekt.com/calstreetskater.flv”);
playButton.onRelease = function() {
ns.pause();
}
pauseButton.onRelease = function() {
ns.pause();
}
var videoInterval = setInterval(videoStatus,100);
var amountLoaded:Number;
var duration:Number;
ns["onMetaData"] = function(obj) {
duration = obj.duration;
}
function videoStatus() {
amountLoaded = ns.bytesLoaded/ns.bytesTotal;
loader.loadbar._width = amountLoaded * 291;
loader.scrub._x = ns.time / duration * 291;
}
var scrubInterval;
loader.scrub.onPress = function () {
clearInterval (videoInterval);
scrubInterval = setInterval (scrubit,10);
this.startDrag(false,-0.5,this._y,289,this._y);
}
loader.scrub.onRelease = loader.scrub.onReleaseOutside = function() {
clearInterval (scrubInterval);
videoInterval = setInterval(videoStatus,100);
this.stopDrag();
}
function scrubit() {
ns.seek(Math.floor((loader.scrub._x/289) *duration));
}
I’ve been using this code for over 2 years with Flash 8. After upgrading to Flash CS3 – I’ve tried using this code (AS2) – and whenever I try to save my movie I get this error now:
The class or interface ‘Timecode’ could not be loaded.
The error shows this line of code as the problem:
var tc:Timecode = new Timecode(ns);
Any suggestions?
Here is a package for as3 that includes frames:
package{
import flash.net.NetStream;
public class Timecode {
private var ns:NetStream;
public function Timecode(ns:NetStream) {
this.ns = ns;
}
public function getTimecode():String {
var time: Number = this.ns.time;
var min:Number = Math.floor(time/60);
var sec:Number = Math.round(time%60);
var milli:Number = time – Math.floor(time);
var frame:Number = Math.round(milli*this.ns.currentFPS);
var ssec : String = sec.toString();
var smin : String = min.toString();
var sframe: String = frame.toString();
if (smin.length < 2)
smin = “0″ + smin;
if (ssec.length < 2)
ssec = “0″ + ssec;
if (sframe.length < 2)
sframe = “0″ + sframe;
var tc:String = smin + “:” + ssec + “:” + sframe;
return tc;
}
}
}
Cheers, Lee – and thanks for the AS3 version Greg, appreciate it
Hi
I’m very new to Flash and Dreamweaver and I’m self taught in both. Most of the stuff you mentioned above I don’t know what to do or how.
I’m trying to make a website where my clients can review my videos and make comments. (See
http://www.magicpro.co.za/Pic/Review Website.jpg
for sample).
I could add my video to a web page, and previewed in a web browser and it works, but it has standard controls with no time-code. Don’t know how to get time-code in (HOURS:MINUTES:SECONDS:FRAMES) don’t know how to to recall current time-code when users selects the “Mark In” button…
The reason for the User and Email fields are, I want to send a mail to predefined users and copy UserX in the mail with the all annotations they entered when they press the update button.
Apart from adding the clip into page I don’t where to start, what to do… Any assistance would be great.
Thanks
I am searching a function that converts time (seconds) to the number, the opposite of the function that is described in this post
Does somebody know about a function similar?
Best regards
Hi,
Until now I used the AS2 version of the code without any problem.
However, I now need to apply the closed caption effect available in CS3 and require to upgrade all code in my fla file to AS3.
I have tried using Greg’s package code but it doesn’t work. I’m not great with actionscript and would really appreciate some help.
Without the timecode script, the closed captioning works great. With the timecode script, nothing works and all elements blink on the page.
Here is the code I used for AS3 from the trail above but don’t think it’s correct…
//Scrollbar menu
display.playPauseButton = player
display.seekBar = seeker
display.muteButton = muter
display.volumeBar = volumer
//display.captionButton = captioner
/************* DISPLAY PLAYING TIME & DURATION *******************/
var tc:Timecode = new Timecode(ns);
timecode_txt.autoSize = true;
timecode_txt.selectable = false;
this.onEnterFrame = function() {
timecode_txt.text = tc.getTimecode();
}
Regards
Thank for the code Lee! Save me a lot of trouble here on the last hour.