在swift中在活动的UITableView单元格中播放视频

问题描述 投票:1回答:2

目标: - 我试图在完全可见的单元格中播放视频(可能是2,3或1)并且应该停止cell的隐身

我正在使用UITableviewAVPlayer。我的tableview充满了视频列表,目前我正在使用捆绑视频, 我使用下面的代码,但它正在播放错误和我们的序列。我在这里做错了什么?用另一种逻辑回答对我来说也没问题。

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()
    }
}

一些链接对我没有帮助

StackOverflow post1

StackOverflow post2

StackOverflow post3

ios iphone swift uitableview xcode9
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.