Pergunta

Now i am trying to get the length of Songs in iOS.

- (NSString *)returnofTotalLength
{
    float duration = [[self.player.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue]/60.0f;
    NSString *length = [NSString stringWithFormat:@"%.2f",duration];;
    NSString *totalLength = [length stringByReplacingOccurrencesOfString:@"." withString:@":"];

    return totalLength;
}

above codes is the total length of song that show like 5:90. You know that 5:90 can't be true because 60 seconds is 1 minute.

It's should be 6:30.

So i want to limit that value for 1 minute (60 seconds).

How can i do it Please help me?

Thanks.

Foi útil?

Solução 4

I just got answer for my own question. Thanks for other answers. :)

NSTimeInterval currentProgress = [[self.player.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue];

    float min = floor(currentProgress/60);
    float sec = round(currentProgress - min * 60);

    NSString *time = [NSString stringWithFormat:@"%02d:%02d", (int)min, (int)sec];
    return time;

That will return NSString with Complete Format.

Outras dicas

This is simple math. Pseudocode:

minutes = (int)(seconds / 60);
rest    = seconds % 60;
result  = minutes:rest

Objc:

int seconds   = 150;
int minutes   = (int)(seconds / 60);
int rest      = seconds % 60;
return [NSString stringWithFormat:@"%i:%i", minutes, rest];

do following :

min=(int)duration/60;
sec=duration%60;

than append minutes and second

If your time crosses to hours then you can go for this :

NSInteger seconds = duration % 60;
NSInteger minutes = (duration / 60) % 60;
NSInteger hours = duration / (60 * 60);
NSString *result = nil;

result = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top