Papa De Beau
Papa De Beau

Reputation: 3828

as3 getting the "hours" in milliseconds in mp3

In As3 the code below gets the minutes and the seconds:

var minutes:uint = Math.floor(PrayPrayer.position / 1000 / 60);
var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;

But what if your listening to an audio talk that goes over the hour mark?

What is the math needed to get the hours from an mp3 talk?

 var hours:uint = Math.floor(PrayPrayer.position / 1000) % 60    & (((???????)));

Upvotes: 0

Views: 1079

Answers (2)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

So PrayPrayer.position is in milliseconds. Your minutes line is dividing by 1000 to get seconds, then dividing by 60 to go from seconds to minutes. Your seconds line is looking at the remainder.

What you started in your hours line is using %, so will look at the remainder - you're using seconds there. % is the modulo operator. It gives you the remainder of integer division. So your line

var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;

is finding the number of seconds (PrayPrayer.position / 1000), which could be something big like 2337, dividing by 60 and just keeping the remainder. 2337/60 = 38 remainder 57, so 2337%60 will be 57.

An easy way to find hours is to use the same trick with your minutes.

var minutes:uint = Math.floor(PrayPrayer.position / 1000 / 60);
var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;
var hours:uint = Math.floor(minutes / 60);
minutes %= 60;  // same as minutes = minutes % 60.  Forces minutes to be between 0 and 59.

Upvotes: 0

this is my conversion method:

    public static var MINUTE:Number = 60;
    public static var HOUR:Number = 60 * MINUTE;
    public static var DAY:Number = 24 * HOUR;

        /**
     * returns string created from seconds value in following format hours:minutes:seconds, i.e. 121 seconds will be displayed as 00:02:01
     * @param   seconds <i>Number</i>
     * @return <i>String</i>
     */
    public static function secondsToHMS(seconds:Number, doNotRound:Boolean = false):String
    {
        var _bNegative:Boolean = seconds < 0;

        seconds = Math.abs(seconds);

        var time:Number = (doNotRound) ? seconds:Math.round(seconds);

        var ms:Number;
        var msec:String;

        if (doNotRound)
        {
            ms = seconds - (seconds | 0);
            msec = prependZeros((ms * 1000) | 0, 3);
        }


        var sec:Number = (time | 0) % MINUTE;

        var min:Number = Math.floor((time / MINUTE) % MINUTE);

        var hrs:Number = Math.floor(time / HOUR);
        //
        return (_bNegative ? "-":"") +
               ((hrs > 9) ? "":"0") + hrs + ":" +
               ((min > 9) ? "":"0") + min + ":" +
               ((sec > 9) ? "":"0") + sec +
               (doNotRound ? "." + msec:"");
    }

prependZeros is another utility to add "0" in front of given string.

Upvotes: 1

Related Questions