【问题标题】:Lag in audio player duration and time elapsed labels音频播放器持续时间和经过时间标签的滞后
【发布时间】:2014-04-03 13:31:01
【问题描述】:

我正在开发的音频播放器上有两个标签。一个是经过的时间,一个是歌曲的持续时间,就像在 iTunes 中一样。

但是,当我玩时,时间会滞后。在 7 秒的歌曲中,经过的时间为“0:00”,持续时间为“-0:07”。然后,当我播放这首歌时,标签会先显示“0:01”和“-0:07”,然后再转到“0:01”和“-0:06”

这是我的设置方式。

-(NSString*)timeFormat:(float)value{

float minutes = lroundf((value)/60);
float seconds = lroundf((value) - (minutes * 60));

int roundedSeconds = (seconds);
int roundedMinutes = (minutes);

NSString *time = [[NSString alloc]
                  initWithFormat:@"%d:%02d",
                  roundedMinutes, roundedSeconds];
return time;

- (void)updateTime:(NSTimer *)timer {


self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                        [self timeFormat:[player currentTime]]];



self.duration.text = [NSString stringWithFormat:@"-%@", [self timeFormat: player.duration - player.currentTime]];

播放按钮会触发这个计时器:

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                  target:self
                                                selector:@selector(updateTime:)
                                                userInfo:nil
                                               repeats:YES];

有什么想法吗?

谢谢。

【问题讨论】:

    标签: ios objective-c cocoa-touch audio nstimer


    【解决方案1】:

    如果其他人遇到此问题,这是最终对我有用的答案。它归结为四舍五入我的当前时间。

    -(NSString*)timeFormat:(float)value{
    
    float minutes = floor(lroundf(value)/60);
    float seconds = lroundf((value) - (minutes * 60));
    
    int roundedSeconds = lroundf(seconds);
    int roundedMinutes = lroundf(minutes);
    
    NSString *time = [[NSString alloc]
                      initWithFormat:@"%d:%02d",
                      roundedMinutes, roundedSeconds];
    return time;
    
    - (void)updateTime:(NSTimer *)timer {
    
    
    self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                            [self timeFormat: ceilf(player.currentTime)]];
    
    
    self.duration.text = [NSString stringWithFormat:@"-%@", [self timeFormat: (player.duration - ceilf(player.currentTime))]];
    
    }
    

    【讨论】:

    • omg 非常感谢,我遇到了完全相同的问题,但无法弄清楚,正要在 SO 上发布一个问题。幸运的是,我首先搜索并找到了您的答案。感谢您至少发布答案! :)
    猜你喜欢
    • 2017-02-10
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多