2017-04-06 76 views
2

以下是我嘗試通過創建自定義控件來播放視頻的代碼。表格視圖在滾動時在其他單元格中播放視頻

單擊播放按鈕時,表格視圖中的視頻將播放,但它也會播放當前正在播放視頻的單元格中的第三個單元格。

在移動到第三個單元格並單擊播放按鈕時,它會播放正確的視頻,但同一個視頻會再次播放兩個單元格。

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

class ViewTableCell: UITableViewCell { 
    var avPlayer: AVPlayer? 
     var avPlayerLayer: AVPlayerLayer? 
     var paused: Bool = false 
     var videoPlayerItem: AVPlayerItem? = nil { 
     didSet { 

      avPlayer?.replaceCurrentItem(with: self.videoPlayerItem) 
     } 
    } 

    var tapAction: ((ViewTableCell) -> Void)? 

    @IBAction func playButtonAction(_ sender: UIButton) { 
     tapAction?(self) 
    } 
func setupMoviePlayer(view:UIView){ 
     self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem) 
     avPlayerLayer = AVPlayerLayer(player: avPlayer) 
     avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect 
     avPlayer?.volume = 3 
     avPlayer?.actionAtItemEnd = .none 
     avPlayerLayer?.frame = view.bounds 
     self.backgroundColor = .clear 
     view.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() 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.setupMoviePlayer(view: videoView) 
     // Initialization code 
    } 
} 

視圖控制器類

var cell : TrendViewTableCell? 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     //MARK CellForRowAt 


     let resusableIdentifier: String = "ViewControllerCell" 
     cell = (tableView.dequeueReusableCell(withIdentifier: resusableIdentifier) as? ViewTableCell) 

     if cell == nil { 
      cell = ViewTableCell(style:UITableViewCellStyle.default, reuseIdentifier: resusableIdentifier) 
     } 

     cell?.videoName.text = ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoTitle 
     cell?.tapAction = { (cell) in 

      self.onTapPlayButton(cell) 

     } 
     cell?.playButton.addTarget(self, action: #selector(TrendViewController.onTapPlayButton), for: .touchUpInside) 
     return cell! 
    } 

func onTapPlayButton(_ selectedCell:ViewTableCell){ 

     if let index = self.trendTableView.indexPath(for: selectedCell){ 
      //put your code to execute here 
      let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[index.row].videoUrl, ofType:"mp4") 

      let path = NSURL.fileURL(withPath: url!) 
      let currentCell = tableView.cellForRow(at: index) 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.setImage(playImage, for: .normal) 

      } else { 
       currentCell.startPlayback() 
       currentCell.playButton.setImage(pauseImage, for: .normal) 
      } 
     } 
    } 
+0

細胞被重用,所以要添加的自來水處理多次爲你滾動。你應該處理單元類中的tap,並通過委託模式或閉包通知tableview控制器; http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11

+0

我已經實施了上面提到的方法。但仍然存在同樣的問題。 @ Paulw11 –

回答

0

你的tableview細胞被重用,這就是你面對這樣的問題的原因。

使用一些布爾值來管理它,或者每次在cellForRow:delegate時重置視頻。

0

在單元初始化之前放置單元格?.playButton.addTarget(self,action:#selector(TrendViewController.onTapPlayButton),用於:.touchUpInside)會更好。

在你的情況,你可以把 「細胞.playButton.addTarget(自我,動作:#selector(TrendViewController.onTapPlayButton)」 這部分是這樣的:如果 細胞==零{ 電池= ViewTableCell(風格:UITableViewCellStyle.default,reuseIdentifier:resusableIdentifier) 細胞.playButton.addTarget(自我,動作:#selector(TrendViewController.onTapPlayButton) } 對於我來說,我會覆蓋在細胞類啓動方法,做在心裏

0

您需要實施:

prepareForReuse() 

你的細胞,那麼這裏面告訴AVPlayerpausereplaceCurrentItemnil或新項目

相關問題