Вопрос

I have two labels on an audio player i'm working on. One is the time elapsed, and one is the duration of the song, just like in iTunes.

However when I play there is a lag in the times. In a 7-second song, the time elapsed reads "0:00" and the duration reads "-0:07". Then when I play the song, for a moment the labels read "0:01" and "-0:07" before going to "0:01" and "-0:06"

Here is how I have things set up.

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

And the play button triggers this timer:

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

Any ideas?

Thanks.

Это было полезно?

Решение

Here is the answer that finally worked for me, if anyone else has had this problem. It boild down to rounding up my currentTime.

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

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top