以下是我尝试通过创建自定义控件来播放视频的代码。
单击播放按钮时,表视图中的视频将播放,但它也会播放视频正在播放的当前单元格的第三个单元格。
在移动到第三个单元格并单击其中的播放按钮时,它会播放正确的视频,但同一视频再次播放两个单元格。
我怎样才能克服这个问题。任何帮助将不胜感激。
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 :(得分:1)
您需要实施:
prepareForReuse()
在您的单元格上,然后在其内部告诉AVPlayer
至pause
或replaceCurrentItem
nil
或新项目
答案 1 :(得分:0)
您的tableview单元格会被重复使用,这就是您遇到此类问题的原因。
使用一些布尔值管理它或每次在cellForRow:delegate时重置视频。
答案 2 :(得分:0)
在单元格启动之前放置单元格会更好吗?.playButton.addTarget(self,action:#selector(TrendViewController.onTapPlayButton),for:.touchUpInside)。
在你的情况下,你可以将" cell?.playButton.addTarget(self,action:#selector(TrendViewController.onTapPlayButton)"放在这个部分中: if cell == nil { cell = ViewTableCell(style:UITableViewCellStyle.default,reuseIdentifier:resusableIdentifier) cell?.playButton.addTarget(self,action:#selector(TrendViewController.onTapPlayButton) } 对我来说,我会覆盖单元类中的启动方法,并在里面做所有事情。