1 Reply Latest reply on Jul 20, 2006 9:29 PM by Newsgroup_User

    Milliseconds to Seconds Minutes

    JSFrench
      is there a way to take a number (specifically the position in milliseconds that a sound has been playing) and convert it into minute:second format? any help?

      jeff
        • 1. Re: Milliseconds to Seconds Minutes
          Level 7
          Jeff,

          > is there a way to take a number (specifically the position in
          > milliseconds that a sound has been playing) and convert it
          > into minute:second format? any help?

          Sure thing. First, you need to bear in mind that mm:hh format is a
          string, not a number, so whatever your usage is going to be -- presumably a
          dynamic text field display, but I'm only guessing -- it needs to be
          compatible with strings.

          Second, you simply need to perform a bit of math. Let's say you have
          the number 87,540. That's 87,540 milliseconds, or 1 minute and 27 seconds
          (and 540 milliseconds, which we can probably just toss). Now, how do we get
          from the one to the other?

          87540 / 1000 gives us seconds ... here, 87.54. Divide that number by 60
          and you have minutes.

          To get seconds -- well, you already did that -- but in time
          applications, seconds need to start over again at 60, so you can use the
          modulus operator, %, to do that for you.

          If you want a leading zero, you may want to use an if() statement to
          check if that value is less than 10.

          var music:Sound = new Sound();
          music.loadSound("audio.mp3", true);
          var minutes:Number = 0;
          var seconds:Number = 0;
          this.onEnterFrame = function() {
          minutes = Math.floor(music.position / 1000 / 60);
          seconds = Math.floor(music.position / 1000) % 60;
          if (seconds < 10) {
          output.text = minutes + ":0" + seconds;
          } else {
          output.text = minutes + ":" + seconds;
          }
          };


          --
          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."