以下是我嘗試通過創建自定義控件來播放視頻的代碼。表格視圖在滾動時在其他單元格中播放視頻
單擊播放按鈕時,表格視圖中的視頻將播放,但它也會播放當前正在播放視頻的單元格中的第三個單元格。
在移動到第三個單元格並單擊播放按鈕時,它會播放正確的視頻,但同一個視頻會再次播放兩個單元格。
我該如何解決這個問題。任何幫助將不勝感激。
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)
}
}
}
細胞被重用,所以要添加的自來水處理多次爲你滾動。你應該處理單元類中的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
我已經實施了上面提到的方法。但仍然存在同樣的問題。 @ Paulw11 –