1
在我的項目中,我有一個播放歌曲的AVAudioPlayer。暫停後,再次播放,重新開始播放,而不是在暫停播放的位置播放。爲什麼會發生? 另外,當我第一次按播放時,它會加載約3-4秒的歌曲。我以爲我通過將它加載到另一個線程來解決它,但它仍然加載得太慢(至少視圖不再凍結)。我該如何解決這個問題?代碼如下:AVAudioPlayer暫停問題
- (IBAction)play:(id)sender {
songIsCurrentlyPaused = NO;
if(songIsCurrentlyPaused==YES){
[self.background play];
} else {
playQueue = dispatch_queue_create("volume_change", NULL);
dispatch_async(playQueue, ^{ NSString *filePath =
[[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
self.background.delegate = self;
[self.background setNumberOfLoops:1];
[self.background setVolume:0.5];
[self.background play]; });
[trackNameLabel setText:@"Currently playing :\n some_song"];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}
}
- (IBAction)pause:(id)sender {
songIsCurrentlyPaused = YES;
[self.background pause];
[trackNameLabel setText:@"Currently playing : some_song (paused)"];
[self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
}
謝謝!
問題解決了!謝謝,非常感謝!但加載速度仍然緩慢:( – nemesis 2012-03-13 22:36:30
@nemesis:我編輯了我的答案,不能編碼,因爲我現在不在我的mac中,它應該可以工作,但它可能需要一些調整。如果你可以在某些後臺線程上移動預加載,那麼應該有(幾乎)沒有延遲 – 2012-03-14 08:36:43
好吧,它可以工作,如果你點擊播放按鈕後,視圖加載一段時間沒有辦法完全刪除延遲?Thanks !! – nemesis 2012-03-14 12:43:21