0

现在我正在尝试获取 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;
}

以上代码是显示like的歌曲总长度5:90。你知道那5:90不可能是真的,因为60秒就是1分钟。

应该6:30

所以我想限制这个值1 minute (60 seconds).

我该怎么做请帮帮我?

谢谢。

4

4 回答 4

1

这是简单的数学。伪代码:

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

对象:

int seconds   = 150;
int minutes   = (int)(seconds / 60);
int rest      = seconds % 60;
return [NSString stringWithFormat:@"%i:%i", minutes, rest];
于 2013-01-17T09:58:51.427 回答
1

执行以下操作:

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

比附加分钟和秒

于 2013-01-17T10:02:25.003 回答
1

如果您的时间超过几个小时,那么您可以这样做:

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];
于 2013-01-17T11:26:54.773 回答
0

我刚刚得到了我自己问题的答案。感谢其他答案。:)

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;

这将返回NSString完整格式。

于 2013-01-17T10:23:29.043 回答