Question

Hello I tried to find a solution in Objective C to get the lenght of audio file included milliseconds. at the moment I have only this code:

-(NSString *) DoubleToNSStringTime:(double)valore
{
    NSNumber *theDouble = [NSNumber numberWithDouble:valore];

    int inputSeconds = [theDouble intValue];

    int hours =  inputSeconds / 3600;
    int minutes = (inputSeconds - hours * 3600 ) / 60; 
    int seconds = inputSeconds - hours * 3600 - minutes * 60; 

    NSString *theTime = [NSString stringWithFormat:@"%.2d:%.2d", minutes, seconds];

    NSLog(@"TDoubleToNSStringTime= %@", theTime);
    return theTime;
}

the Issue is this: I would like to get more precise timePosition, Minutes : Seconds : Milleseconds

some of you has a solution?

Thanks

Was it helpful?

Solution

You don’t need to convert to NSNumber and back:

- (NSString *)DoubleToNSStringTime:(double)valore
{
  NSInteger interval = (NSInteger)valore;
  double milliseconds = (valore - interval) * 1000;
  NSInteger seconds = interval % 60;
  NSInteger minutes = (interval / 60) % 60;
  minutes += (interval / 60);
  return [NSString stringWithFormat:@"%02ld:%02ld:%3.0f", minutes, seconds, milliseconds];
}

That should print something like 60:45:789 for an input of 3645.789.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top