开发者

Convert time in microseconds to minutes and seconds

My iPhone app uses FFMPeg for decoding/playing an audio file.

I'm successfully getting the current time in microseconds from the appropriate FFMPeg function, but I'm not being able to convert it to minutes and seconds (MM:SS).

This is my code to display an NSString containing the time; getFFMPEGtime is the time in microseconds:

 self.currentAudioTime = [NSString stringWithFormat: @"%02d:%02d",
                                (int) getFFMPEGtime / (int)1000000,
                                (int) getFFMPEGtime % 1000000];

It seems that the time in minutes is okay, but t开发者_C百科he seconds are messed up. What's the proper way to convert microseconds to minutes and seconds?


Code:

int microseconds = 59434225;
int seconds = microseconds / 1000000;
NSString * result = [NSString stringWithFormat:@"%02d:%02d",seconds/60,seconds%60];

Output:

63434225 -> 01:03
59434225 -> 00:59


I'd argue that your time in seconds is probably right and your time in minutes is most likely wrong.

(int) getFFMPEGtime / (int)1000000 is giving you total seconds

To get MM:SS, you need to do the following:

self.currentAudioTime = [NSString stringWithFormat: @"%02d:%02d",
                                (int) getFFMPEGtime / (int)60000000,
                                (int) ((getFFMPEGtime % 60000000)/1000000)];


This is math, if you have µs, and one minute has 60s, divide your number by 60000000 and you have your minutes.

The remainder of dividing your number by 60e6 is in µseconds, so divide that by 1e6 to get the seconds.

Example:

Given 123456789 µs, you have:

  1. 123456789 / 60e6 = 2 minutes
  2. ((123456789 % 60e6) / 1e6) = 3.45 seconds
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜