Working on yet another video player and this client DEFINITELY WANTS (big babies) the time to read min:sec versus decimal numbers.
root.pTime.text = ns.time.toFixed(0); root.tTime.text = duration.toFixed(0);
So, I'm using toFixed but I'm wondering if any of your code geniuses can tell me how to convert ns.time (progress time) and duration (video duration) and simple min:sec without separate text boxes for minutes, seconds, colons and slashes for the time readout aka 0:08 / 1:49 in the Flash file with 0:08 being time played and 1:49 being total duration with a slash in between. This is what the Creative Director wants.
I can do the Date() conversions but that seems like a pain in the ass because under 10 seconds I can't get 01, 02 instead of 1,2, etc if you know what I mean.
Any quick solutions? Adobe help sucks ass. I found nothing after an hour that is helpful for use devigners.
You can use date tricks to output something with colon from millisecond input but not sure it is optimal processing wise or if that is what you want?
import flash.utils.Timer;
import flash.events.TimerEvent;
var d: Date = new Date();
var count: int = 0;
var t: Timer= new Timer(100, 0);
function traceTime( e: TimerEvent )
{
count++;
var millisec:int = count*100;
trace( millisec )
trace( formatWithColons( millisec ) );
}
function formatWithColons( tim: int )
{
var t2: int = d.getTime();
var d2: Date = new Date( new Date(t2+ tim ).getTime() - t2);
var str: String = d2.toString().split(' ')[3];
// you prob don't want the first 0?
///return str.substr(-4,4);
return str.substr(-5,5);
}
t.addEventListener( TimerEvent.TIMER, traceTime );
t.start();
I'm not good at explaining my requests. Thanks JLM. That is exactly what I wanted, but like you said, I don't think it would be optimal doing that every frame. And honestly, I was hoping there might be something new in AS3 and I missed it where there was a convertToColonBasedTime() (or something) function already written by Adobe.
I'm just going to go with the milliseconds. I've put enough effort into this Video Player.
Thanks JLM. You are a code genius.
package com.snepo.videoplayer.util {
/**
* @author andrewwright
*/
public class TimeUtil
{
/** returns time in hh:mm:ss format from seconds **/
public static function formatTime ( time:Number ):String
{
var remainder:Number;
var hours:Number = time / ( 60 * 60 );
remainder = hours - (Math.floor ( hours ));
hours = Math.floor ( hours );
var minutes = remainder * 60;
remainder = minutes - (Math.floor ( minutes ));
minutes = Math.floor ( minutes );
var seconds = remainder * 60;
remainder = seconds - (Math.floor ( seconds ));
seconds = Math.floor ( seconds );
var hString:String = hours < 10 ? "0" + hours : "" + hours;
var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;
if ( time < 0 || isNaN(time)) return "00:00";
if ( hours > 0 )
{
return hString + ":" + mString + ":" + sString;
}else
{
return mString + ":" + sString;
}
}
}
}
hope it helps,
lith
fuck you guys are brilliant
thanks
lith, I used yours. It was easier to understand how to translate what I had already done into the conversion.