Search code examples
ioscocoa-touchmpmediaitem

How to limit float Value in iOS


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.


Solution

  • 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.