2012-03-13 105 views
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]; 
} 

謝謝!

回答

1

你在play:

開始設置songIsCurrentlyPausedNO嘗試註釋掉它:

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

如果你想擺脫你要去,最初的暫停已經完全重組您的播放器設置。在用戶點擊播放按鈕之前進行初始化,並且還可以調用:

[self.background prepareToPlay]; 

它將預加載歌曲。還要將songIsCurrentlyPaused = NO;移動到代碼中較早的地方。

編輯:

爲了擺脫,你應該將你的初始化代碼到地方,比如loadViewviweDidLoad初始延遲。

//initialization code 
    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 prepareToPlay]; 

現在,這可能會導致在UI顯示滯後,所以你可能wan't考慮在後臺線程預加載數據 。然後

IBAction方法應該進行修改:與暫停

- (IBAction)play:(id)sender 
{ 

    if (songIsCurrentlyPaused==YES) 
    { 
     [self.background play]; 
    } 
    else 
    { 
     playQueue = dispatch_queue_create("volume_change", NULL); 
     dispatch_async(playQueue, ^{ 
     [self.background setCurrentTime: 0.0]; 
     [self.background play]; 
     }); 

     [self.progressBar setProgress:0.0 animated:YES]; 
     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 

    } 
    songIsCurrentlyPaused = NO; 
} 

- (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]; 
} 
+0

問題解決了!謝謝,非常感謝!但加載速度仍然緩慢:( – nemesis 2012-03-13 22:36:30

+0

@nemesis:我編輯了我的答案,不能編碼,因爲我現在不在我的mac中,它應該可以工作,但它可能需要一些調整。如果你可以在某些後臺線程上移動預加載,那麼應該有(幾乎)沒有延遲 – 2012-03-14 08:36:43

+0

好吧,它可以工作,如果你點擊播放按鈕後,視圖加載一段時間沒有辦法完全刪除延遲?Thanks !! – nemesis 2012-03-14 12:43:21