2012-11-06 64 views
1

上的iOS應用程序的工作,我在玩使用AVPlayer視頻,我使用同步滑塊與playerItem電流持續時間同步:AVPlayer與UISlider iOS中

//synchronizing the current time label and slider with the player. 
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 100) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) 

{ 
    CMTime endTime = CMTimeConvertScale (self.player.currentItem.asset.duration, self.player.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero); 
    if (CMTimeCompare(endTime, kCMTimeZero) != 0) { 
     double normalizedTime = (double) self.player.currentTime.value/(double) endTime.value; 
     self.slider.value = normalizedTime; 
    } 
    Float64 currentSeconds = CMTimeGetSeconds(self.player.currentTime); 
    int mins = currentSeconds/60.0; 
    int secs = fmodf(currentSeconds, 60.0); 
    NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins]; 
    NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs]; 
    self.currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minsString, secsString]; 
}]; 

的問題是,我玩視頻在Modal視圖中,當我按下按鈕關閉視圖控制器時,視圖控制器關閉,但視頻繼續在後臺播放(因爲可以聽到資源的音頻)。我該如何解決這個問題?我確信它與我正在使用的隊列有關,但我已經嘗試了主隊列和併發隊列,但每次結果都是一樣的。

回答

0

我想你需要通過調用cancelReading方法來停止AVAssetReader

文檔:

cancelReading

取消任何後臺工作,並防止接收機的輸出 從閱讀更多的樣品。

  • (無效)cancelReading討論如果要停止達到其時間範圍結束前讀取來自接收器樣品,你 應該調用此方法來阻止任何後臺預讀取操作 的可能已經在進展。

此外,當您關閉模式的看法,你應該使用的AVPlayerremoveTimeObserver方法。

也有pause AVPlayer的方法,如果你沒有AVAssetReader

+0

什麼應該是「removeTimeObserver:(id)observer」中的參數觀察者? –

+0

它必須是您的調用返回值'addPeriodicTimeObserverForInterval:queue:usingBlock:' –

+0

我怎樣才能得到由方法addPeriodicTimeObserverForInterval:queue:usingBlock返回的值:在上面提到的代碼? –