2016-02-16 15 views
3

我想用AVPlayer創建一個視頻播放器。來自YouTube的視頻加載。我希望視頻當前時間發生變化時,我的uiSlider和我的時間標籤可以更新。我正在使用「observeValueForKeyPath」方法從播放器項目中捕獲「loadedTimeRanges」狀態。這裏是我的代碼:swift observevalueforkeypath + loadedTimeRanges只能稱爲受限時間

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
     print("keyPath: \(keyPath)") 
     let playerItem:AVPlayerItem = object as! AVPlayerItem 
     if keyPath == "status" { 
      if playerItem.status == AVPlayerItemStatus.ReadyToPlay{ 
       print("AVPlayerItemStatusReadyToPlay") 
      } else if playerItem.status == AVPlayerItemStatus.Failed { 
       print("AVPlayerItemStatusFailed") 
      } 
     } else if keyPath == "loadedTimeRanges" { 
      let currentTime = playerItem.currentTime().seconds 
      let totalDuration = playerItem.duration.seconds 
      print("value: \(Float(currentTime/totalDuration))") 
      self.monitoringPlayback(player.currentItem!) 
      self.slider.setValue(Float(currentTime/totalDuration), animated: false) 
     } 
    } 

    func monitoringPlayback(playerItem:AVPlayerItem) { 
     let currentSecond:Double = playerItem.currentTime().seconds 
     self.updateVideoSlider(currentSecond) 
     let timeString = self.updateTime(playerItem.duration.seconds - currentSecond) 
     print("time string: \(timeString)") 
     self.lblTime.text = timeString 
    } 

但是,「observeValueForKeyPath」方法每次只調用20-22次。請檢查日誌截圖:https://gyazo.com/5ca57ba532689d83aea855ae41387f53 這是我第一次使用這種方法,所以也許我不明白它是如何工作的。如果有人知道它,請告訴我爲什麼。感謝您閱讀我的問題。

+0

如果你想知道當前回放時間你爲什麼不在'AVPlayer'上使用'currentTime'方法? 。現在你正在觀察已經加載的項目的時間範圍。一旦你用盡了大塊加載你停止收到通知。 –

+0

但我怎麼知道時間變化事件? – Ashley

+0

來自文檔'這個屬性不是關鍵值可觀察的;使用addPeriodicTimeObserverForInterval:queue:usingBlock:或addBoundaryTimeObserverForTimes:queue:usingBlock:instead.' –

回答

-1

它,容易

VAR rightCurrentTimer:的UILabel = {

let lbl = UILabel() 
    lbl.text = "00:00" 
    lbl.textColor = .white 
    lbl.translatesAutoresizingMaskIntoConstraints = false 
    lbl.font = UIFont.systemFont(ofSize: 11) 
    return lbl 

}() 

let leftWhatTimePlayed:UILabel = { 

    let lbl = UILabel() 
    lbl.translatesAutoresizingMaskIntoConstraints = false 
    lbl.textColor = .white 
    lbl.text = "00:00" 
    lbl.font = UIFont.systemFont(ofSize: 11) 
    return lbl 

}() 

設間隔= CMTime(價值:1,時間表:1)

player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (timeProgress) in 

     let secondsPlay = CMTimeGetSeconds(timeProgress) 
     let secondString = String(format:"%02d" , Int(secondsPlay) % 60) 
     let minutesString = String(format:"%02d", Int(secondsPlay)/60) 

     self.leftWhatTimePlayed.text = "\(minutesString):\(secondString)" 

     if let duration = self.player?.currentItem?.duration { 

      let totalSecond = CMTimeGetSeconds(duration) 

      let whatSecondIsPlay = totalSecond - secondsPlay 

      let secondsIsPlay = String(format:"%02d" , Int(whatSecondIsPlay) % 60) 
      let minutesIsPlay = String(format: "%02d", Int(whatSecondIsPlay)/60) 

      self.rightCurrentTimer.text = "\(minutesIsPlay):\(secondsIsPlay)" 
      self.videoLengthTimeSlider.value = Float(secondsPlay/totalSecond) 

     } 


    })