【问题标题】:How to limit float Value in iOS如何在 iOS 中限制浮点值
【发布时间】:2013-01-17 09:54:45
【问题描述】:

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

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

应该是6:30

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

我该怎么做请帮帮我?

谢谢。

【问题讨论】:

    标签: ios cocoa-touch mpmediaitem


    【解决方案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];
    

    【讨论】:

      【解决方案2】:

      执行以下操作:

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

      比附加分钟和秒

      【讨论】:

        【解决方案3】:

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

        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];
        

        【讨论】:

          【解决方案4】:

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

          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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-04-18
            • 2012-04-04
            • 1970-01-01
            • 1970-01-01
            • 2013-08-20
            • 1970-01-01
            • 2010-09-29
            • 1970-01-01
            相关资源
            最近更新 更多