AVAudioPlayer - 在 UILabel 上显示倒计时器

发布于 2024-11-08 06:54:16 字数 621 浏览 0 评论 0原文

我有一个倒计时器,可以显示音轨上剩余的秒数。由于某种原因,倒计时不是以 1 秒的间隔计数,而是第一次计数 2 秒,然后下一次计数 1 秒。 (它以这种模式计数 - 它会跳 2 秒,然后一遍又一遍地跳 1 秒)。

这是我的代码:

// display current time left on the track
- (void)updateTimeLeft {
    NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

    // update your UI with timeLeft
    self.timeDisplay.text = [NSString stringWithFormat:@"%.2f", timeLeft / 60];

}

这是我的 NSTimer:

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES];

感谢您的帮助。

I have a countdown timer that displays the number of seconds left on a audio track. For some reason the countdown is not counting at 1 second intervals, but 2 seconds on the first count and then 1 second on the next count. (It's counting in this pattern - it jumps 2 seconds, then 1 over and over).

here's my code:

// display current time left on the track
- (void)updateTimeLeft {
    NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

    // update your UI with timeLeft
    self.timeDisplay.text = [NSString stringWithFormat:@"%.2f", timeLeft / 60];

}

and here's my NSTimer:

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES];

thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韵柒 2024-11-15 06:54:16

试试这个

 - (void)updateTimeLeft
{
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

int min=timeLeft/60;

int sec = lroundf(timeLeft) % 60;

// update your UI with timeLeft
self. timeDisplay.text = [NSString stringWithFormat:@"%d minutes %d seconds", min,sec];
}

只是一个想法

try this

 - (void)updateTimeLeft
{
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

int min=timeLeft/60;

int sec = lroundf(timeLeft) % 60;

// update your UI with timeLeft
self. timeDisplay.text = [NSString stringWithFormat:@"%d minutes %d seconds", min,sec];
}

just an idea

羁拥 2024-11-15 06:54:16
 - (void)updateTimeLeft
 {
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

// update your UI with timeLeft
self. timeDisplay.text = [NSString stringWithFormat:@"%f seconds left", timeLeft];
}

尝试这样...它只会在几秒钟内显示

 - (void)updateTimeLeft
 {
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

// update your UI with timeLeft
self. timeDisplay.text = [NSString stringWithFormat:@"%f seconds left", timeLeft];
}

try like this...it will display only in seconds

断肠人 2024-11-15 06:54:16

使用系统格式化的最佳方法:

    - (NSString*)updateTimeLeft
    {
        float current = CMTimeGetSeconds(_player.currentItem.currentTime);
        float duration = CMTimeGetSeconds(_player.currentItem.duration);

        if(!isnan(current) && !isnan(duration)){
             NSTimeInterval timeLeft = duration - current;    
             NSTimeInterval durationInSeconds = timeLeft;
             NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
             formatter.allowedUnits = NSCalendarUnitMinute | NSCalendarUnitSecond;
             formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
             NSString *string = [formatter stringFromTimeInterval:durationInSeconds];

             return string;
         }

     return nil;
    }

Best way using system formate:

    - (NSString*)updateTimeLeft
    {
        float current = CMTimeGetSeconds(_player.currentItem.currentTime);
        float duration = CMTimeGetSeconds(_player.currentItem.duration);

        if(!isnan(current) && !isnan(duration)){
             NSTimeInterval timeLeft = duration - current;    
             NSTimeInterval durationInSeconds = timeLeft;
             NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
             formatter.allowedUnits = NSCalendarUnitMinute | NSCalendarUnitSecond;
             formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
             NSString *string = [formatter stringFromTimeInterval:durationInSeconds];

             return string;
         }

     return nil;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文