2011-10-24 59 views
2

我目前正在編寫基於蘋果電腦的音樂應用程序,並且我想爲其添加一個進度欄。音樂進度條

如何使進度條顯示在我的應用程序中默認iOS的iPod應用程序中播放的音樂的進度?

+0

你在用什麼來播放音樂? –

+0

我的應用程序顯示默認的iPod應用程序的信息,所以iPod應用程序正在播放音樂 –

回答

3

您可能想看看MPMusicPlayerController類參考 - 特別是currentPlayBackTime屬性。從來沒有玩過它,但看起來你想要採取並在界面中以某種方式呈現它。

2

添加一個計時器火每秒(1.0),並把它調用的方法來更新類似下面的進度條:

- (void) updateProgressBar:(NSTimer *) timer { 
    if (currentPlaybackTime != lengthOfSong) { 
     [self.progressBar setValue:self.musicPlayer.currentTime]; 
    } 
    else { 
     [timer invalidate], timer = nil; 
     [self.progressBar setValue:lengthOfSong]; 
    } 
} 
3

這就是我如何完成這個 在頭文件中我聲明瞭這些變量。

NSTimer * currentTimeUpdateTimer;

'UISlider *timeSlider;

BOOL userIsScrubbing;

- (IBAction)handleScrubberTouchDown:(id)sender;

- (IBAction)handleScrubberTouchUp:(id)sender;

- (IBAction)handleScrub:(id)sender;

在主文件現在viewDidLoad方法下,我跑了一個NSTimer currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateCurrentiPodItemTime) userInfo:NULL repeats:YES];

userIsScrubbing = NO;

之後,這是更新一切的問題。

- (void)updateCurrentiPodItemTime { 
    MPMediaItem *nowPlayingItem = [self.musicPlayer nowPlayingItem]; 

if (nowPlayingItem == nil) { 
    self.minLabel.text = @"00:00"; 

} else { 
    NSNumber *duration = [nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
    double currentTime = self.musicPlayer.currentPlaybackTime; 
    self.minLabel.text = [NSString stringWithFormat: @"%02d:%02d", (int) currentTime/60, (int) currentTime % 60];  
    self.maxLabel.text = [NSString stringWithFormat: @"%02d:%02d", [duration intValue]/60, [duration intValue] % 60]; 

    if (!userIsScrubbing) 
     self.timeSlider.value = (float) currentTime; 
    } 
} 

- (IBAction)handleScrubberTouchDown:(id)sender { 
    userIsScrubbing = YES; 
} 

- (IBAction)handleScrubberTouchUp:(id)sender { 
    userIsScrubbing = NO; 
} 

- (IBAction)handleScrub:(id)sender { 
    self.musicPlayer.currentPlaybackTime = timeSlider.value; 
}