2014-04-03 47 views
0

我正在處理的音頻播放器上有兩個標籤。一個是經過的時間,一個是歌曲的持續時間,就像在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]; 

任何想法?

謝謝。

回答

2

這是最後爲我工作的答案,如果其他人有這個問題。它下降到四捨五入當前時間。

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

} 
+0

omg非常感謝你,我有EXACT相同的問題,並不能解決它,並且即將發佈一個關於SO的問題。幸運的是,我先搜索並找到了答案。感謝至少張貼一個答案! :) – Pangu