Play video in active UITableView's cells in swift

时光总嘲笑我的痴心妄想 提交于 2020-01-01 14:05:47

问题


Target :- I am trying to play video in fully visible cell (it may be 2,3 or 1) & it should be stop with invisibility of cell

I am using UITableview with AVPlayer. My tableview is full of video listing currently i am using videos from bundle,
I used below code, But it's playing incorrectly & weired sequence. What i am doing wrong here? Answer with another logic is also fine for me.

ViewController.swift -

protocol VideoActivityDelegate {
    func startVideo()
    func stopVideo()
}

// MARK: TableView datasource delegate
var videoDelegate: VideoActivityDelegate?

extension ViewController :UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! VideoTableViewCell
        videoDelegate = cell
        cell.selectionStyle = .none
        cell.videoPlayerItem = AVPlayerItem.init(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video\(indexPath.row+1)", ofType: ".mp4")!))
        return cell
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        //print("scrollViewWillBeginDragging")
        for indexes in tableViewHome.indexPathsForVisibleRows! {
            let  cellRect = tableViewHome.rectForRow(at: indexes)
            if tableViewHome.bounds.contains(cellRect) {
            }
        }
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print("displaing ---> \(indexPath.row)")
        videoDelegate?.startVideo()
    }
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
         print("Ending  \(indexPath.row)")
        //videoDelegate?.stopVideo()
    }
}

VideoTableViewCell.Swift

class VideoTableViewCell: UITableViewCell {
    @IBOutlet weak var videoView: UIView!
    var avPlayer: AVPlayer?
    var avPlayerLayer: AVPlayerLayer?
    var videoPlayerItem: AVPlayerItem? = nil {
        didSet {
            avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setupMoviePlayer()
    }

    func setupMoviePlayer(){
        self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
        avPlayer?.volume = 3
        avPlayer?.actionAtItemEnd = .none
        avPlayerLayer?.frame = videoView.bounds

        //self.backgroundColor = .clear
        self.videoView.layer.insertSublayer(avPlayerLayer!, at: 0)

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.playerItemDidReachEnd(notification:)),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                               object: avPlayer?.currentItem)
    }
    @objc func playerItemDidReachEnd(notification: Notification) {
        let p: AVPlayerItem = notification.object as! AVPlayerItem
        p.seek(to: kCMTimeZero, completionHandler: nil)
    }
}
extension VideoTableViewCell :VideoActivityDelegate {
    func startVideo(){
        self.avPlayer?.play()
    }
    func stopVideo() {
        self.avPlayer?.pause()
    }
}

Some link does not helps me

StackOverflow post1

StackOverflow post2

StackOverflow post3

来源:https://stackoverflow.com/questions/47076230/play-video-in-active-uitableviews-cells-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!