2017-04-13 208 views
0

我有一個uiView,我正在播放視頻與自定義控件。視頻停止播放,而不是暫停播放,迅速AVPlayer

當我暫停視頻,它進入了視頻的開始,暫停,而不是在特定的框架

繼暫停是我的代碼

func playButtonTapped(cell: ViewTableCell) { 
     guard let indexPath = self.tableView.indexPath(for: cell) else { 
      return 
     } 
     let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoUrl, ofType:"mp4") 

     let path = NSURL.fileURL(withPath: url!) 
     currentCell = tableView.cellForRow(at: indexPath) as! ViewTableCell 
     currentCell.videoPlayerItem = AVPlayerItem.init(url: path) 

     let playImage = UIImage(named: "image_video_play") as UIImage? 
     let pauseImage = UIImage(named: "image_video_pause") as UIImage? 
     if currentCell.avPlayer?.rate == 1.0 { 
      currentCell.stopPlayback() 
      currentCell.playButton.isHidden = false 
      currentCell.playButton.setImage(playImage, for: .normal) 

     } else { 
      currentCell.startPlayback() 
      currentCell.playButton.isHidden = true 
      currentCell.playButton.setImage(pauseImage, for: .normal) 
     } 

    } 

我在建立錄像機ViewTableCell類如下

var avPlayer: AVPlayer? 
    var avPlayerLayer: AVPlayerLayer? 
    var videoPlayerItem: AVPlayerItem? = nil { 
     didSet { 

      avPlayer?.replaceCurrentItem(with: self.videoPlayerItem) 
     } 
    } 
    @IBAction func playButtonAction(_ sender: UIButton) { 
     self.delegate?.playButtonTapped(cell: self) 
    } 


    func setupMoviePlayer(){ 
     self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem) 
     avPlayerLayer = AVPlayerLayer(player: avPlayer) 
     avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect 
     avPlayer?.volume = 3 
     avPlayer?.actionAtItemEnd = .none 
     avPlayerLayer?.frame = videoView.bounds 
     self.backgroundColor = .clear 
     videoView.layer.insertSublayer(avPlayerLayer!, at: 0) 

     let interval = CMTime(value: 1, timescale: 2) 
     avPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using :{ (progressTime) in 
      let seconds = CMTimeGetSeconds(progressTime) 

      if let duration = self.avPlayer?.currentItem?.duration{ 
       let durationSeconds = CMTimeGetSeconds(duration) 
       self.videoSlider.value = Float(seconds/durationSeconds) 
      } 
     }) 

    } 

    func stopPlayback(){ 
     self.avPlayer?.pause() 
    } 
    func startPlayback(){ 
     self.avPlayer?.play() 
    } 

的問題是每個上的播放按鈕「currentCell.videoPlayerItem = AVPlayerItem.init(網址:路徑)」的點擊我被調用的是替換videoPlayerItem。 我想在特定的幀中暫停視頻,而不是回到視頻的開頭。

我該如何解決這個問題。任何幫助將不勝感激。

+0

當暫停視頻播放率將爲0。而你在1 else條件認沽stopPlayback率設定stopPlayback,它會工作。 –

回答

0

每次按下按鈕時都替換videoPlayerItem。這就是爲什麼它復位:

avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)