views:

215

answers:

4

Hi there! what about this one:

I want to format the currentTime displayed by a videoPlayer component inside flex, something like : 8230.999 to something like 01:59:59:999 which is "hours:minutes:seconds:milliseconds"

I trie different sets of codes but they can't get it to work because currentTime is nor a correct miliseconds time as it adds a floating 3 digit point to seconds;

so instead of : 2000ms it outputs 2.000

something people like me just can't understand!

thanx for any help :)

### UPDATE

I still have problem with milliseconds. here's the current MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            protected function convert_clickHandler(event:MouseEvent):void
            {
                var val:Number = new Number(inPut.text); //inPut.text = 1000.001

                //val = val * 1000;
                outPut.text = timeFormat(val);
            }
            public static function timeFormat(value:Number):String
            {
                var milliseconds:Number  = value % 1000;
                var seconds:Number  = Math.floor((value/1000) % 60);
                var minutes:Number  = Math.floor((value/60000) % 60);
                var hours:Number  = Math.floor((value/3600000) % 24);

                var s_miliseconds:String     = (milliseconds<10 ? "00" : (milliseconds<100 ? "0" : ""))+ String(milliseconds);
                var s_seconds:String     = seconds < 10 ? "0" + String(seconds) : String(seconds); 
                var s_minutes:String     = minutes < 10 ? "0" + String(minutes) : String(minutes); 
                var s_hours:String     = hours < 10 ? "0" + String(hours) : String(hours);

                return s_hours  + ":" + s_minutes  + ":" + s_seconds + '.'+s_miliseconds;
                // returns 00:00:01.000.0009999999999763531 should return 00:00:01.001
                // I still have problem with milliseconds
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:TextInput x="240" y="72" id="inPut" text="1000.001"/>
    <s:TextInput x="240" y="140" id="outPut"/>
    <s:Button x="274" y="107" label="convert" id="convert" click="convert_clickHandler(event)"/>
</s:Application>
A: 

A correct one:

I made this one. A complete class.

package com.strangemother.utils
{
    public class Time extends Object
    {

        public function Time()
        {
        }

        public static function timeFormat(value:int):String
        {
            var seconds:Number  = value % 60;
            var minutes:Number  = Math.floor((value % 3600 ) /60);

            var minuteString:String     = minutes < 10 ? "0" + String(minutes) + ":" : String(minutes) + ":";   
            var secondString:String     = seconds < 10 ? "0" + String(seconds) : String(seconds);
            return minuteString + secondString;
        }


    }
}

this works - Googled it.

var secondsPerMinute = 60;
var minutesPerHour = 60;
function convertSecondsToHHMMSS(intSecondsToConvert) {
var hours = convertHours(intSecondsToConvert);
var minutes = getRemainingMinutes(intSecondsToConvert);
minutes = (minutes == 60) ? "00" : minutes;
var seconds = getRemainingSeconds(intSecondsToConvert);
return hours+" : "+minutes+" : "+seconds;
}
function convertHours(intSeconds) {
var minutes = convertMinutes(intSeconds);
var hours = Math.floor(minutes/minutesPerHour);
return hours;
}
function convertMinutes(intSeconds) {
return Math.floor(intSeconds/secondsPerMinute);
}
function getRemainingSeconds(intTotalSeconds) {
return (intTotalSeconds%secondsPerMinute);
}
function getRemainingMinutes(intSeconds) {
var intTotalMinutes = convertMinutes(intSeconds);
return (intTotalMinutes%minutesPerHour);
}

trace(convertSecondsToHHMMSS(13345)); 
Glycerine
Thanx Glycerin, but the code you provided doesn't work:I hade to declare vars as int (var hours:int) and also the miliseconds format s not what flash video ouputs, as I said above, the miliseconds formt contains a 3 digits after the float, something like:trace(convertSecondsToHHMMSS(13345.123)); but thanx anyway :)
numediaweb
When I get home tonight I know of another that'll work if you still need the answer... In fact I know where I can get the code. - 15 minutes I'll have it for you dude.
Glycerine
I'm very sorry - I notice you need hours as well as minutes and seconds. I'll recode that class for you tonight if no one else does it.
Glycerine
thanx again man, unfortuntly this also didn't return the correct format : i used timeFormat(1.500) which is 1 second and a half, instead of returning 00:01 it returned 25:00. the best was to return 00:00:01:500. Thanx again
numediaweb
No way?! I use this live. I'll check it tonight for you. I'll try and make a working one.
Glycerine
I updated the post above using the function you sent. the only problem now is with milliseconds.
numediaweb
A: 

I tend to use a Date formatter to generate the proper output...

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
    <mx:Script>
    <![CDATA[
        public var seconds:Number = 10000;

        private function formatTime(event:MouseEvent):void
        {
            var stamp:Date = new Date(0, 0, 0, 0, 0, seconds, 0)
            txtTime.text = fmtTime.format(stamp); 
        }
     ]]>
     </mx:Script>
     <mx:DateFormatter id="fmtTime" formatString="JJ:NN:SS"/>
     <mx:Text id="txtTime" text=""/>
     <mx:Button click="formatTime(event)"/>
</mx:Application>

I hope that helps you out.

Zaren
Thanx Zaren but this doesn't solve my problem. i'm working with a special time format which is the length of the video, it's not a date format because the video time can be 30:00:00:000 (hh:mm:ss:mis) so date formatter is not working. flash video returns miliseconds in a special format, so instead of 10000 (10 seconds) it returns 10000.000 (notice the 3 floating digits).thank you anyway :)
numediaweb
A: 

Thanx to the help of both Glycerine and Zaren and by combining their suggestions, I come up with this class:

public class Format
    {

        public static function currentTime(value:Number):String //example value in mis: 1000
        {
            value = value * 1000; // convert video currentTime 1.000 to 1000

            var milliseconds:Number  = Math.round(((value/1000) % 1) * 1000);
            var seconds:Number  = Math.floor((value/1000) % 60);
            var minutes:Number  = Math.floor((value/60000) % 60);
            var hours:Number  = Math.floor((value/3600000) % 24);

            var s_miliseconds:String     = (milliseconds<10 ? "00" : (milliseconds<100 ? "0" : ""))+ String(milliseconds);
            var s_seconds:String     = seconds < 10 ? "0" + String(seconds) : String(seconds); 
            var s_minutes:String     = minutes < 10 ? "0" + String(minutes) : String(minutes); 
            var s_hours:String     = hours < 10 ? "0" + String(hours) : String(hours);

            return s_hours  + ":" + s_minutes  + ":" + s_seconds + '.'+s_miliseconds;

        }
    }

I use it like this:

// caculate playhead time       
var value:Number = new Number(video_player_monitor.currentTime);
imghri_playhead.text = Format.currentTime(value);

Hope it helps someone else. Again, Thank you guys for your support ;-)

numediaweb
A: 

I needed to convert a string like "00:02:31:76" to a number of seconds. If anyone is interested, here's the class for that:


package {

public class ConvertStringToTime {

    public static function processTimeString(timeString:String):Number {

        var timePieceArray:Array = timeString.split(":");

        var milliseconds:Number = Number(timePieceArray[3])/100;
        var seconds:Number = Number(timePieceArray[2]);
        var minutesInSeconds:Number = Number(timePieceArray[1])*60;
        var hoursInSeconds:Number = Number(timePieceArray[0])*60*60;

        var timeInSeconds:Number = hoursInSeconds + minutesInSeconds + seconds + milliseconds

        return timeInSeconds;
    }

}

}

Then, if I want to use it, I can call, for example: trace(ConvertStringToTime.processTimeString("00:02:03:25"));

Darren