如何解决UICollectionViewCell重用导致视频继续播放的问题

问题描述

我阅读了以下文章:

I am loading videos in AVPlayer in collection view but it repeats some cells data

我的尝试:

控制器:

    let vidos = ["v1","v2","v3"]

    let collection : UICollectionView = {
        let layout  = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        let colle = UICollectionView.init(frame: .zero,collectionViewLayout: layout)
        colle.translatesAutoresizingMaskIntoConstraints = false
        colle.isPagingEnabled = true
        colle.showsVerticalScrollIndicator = false
        return colle
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setUpView()
    }
    func setUpView(){
        collection.delegate = self;
        collection.dataSource = self;
        collection.register(vieosCell.self,forCellWithReuseIdentifier: collId)
        collection.backgroundColor = UIColor.darkGray
        view.addSubview(collection);
        
        collection.snp.makeConstraints { (make) in
            make.top.left.equalTo(view)
            make.bottom.right.equalTo(view)
        }
        
    }

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return vidos.count
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collId,for: indexPath) as! vieosCell
//        cell.imagSrc = images[indexPath.row]
//        print("111",indexPath.row)
        cell.vido_url = vidos[indexPath.row]
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width,height:view.frame.height )
    }
    func collectionView(_ collectionView: UICollectionView,minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

单元格:

    var vido_url : String?{
        didSet{
            guard let src = vido_url else{
                return
            }
            loadVideo(src)
        }
    }
    
    var player : AVPlayer?
    var plyaerLayer : AVPlayerLayer?
    
    func loadVideo(_ url:String){
        
        guard let path = Bundle.main.path(forResource: url,ofType: "mp4") else {
            return
        }
        player = AVPlayer(url: URL(fileURLWithPath:path))
        plyaerLayer = AVPlayerLayer(player: player)
        plyaerLayer?.frame = contentView.bounds;
        plyaerLayer?.videoGravity = .resizeAspectFill
        contentView.layer.addSublayer(plyaerLayer!)
        player?.play()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        plyaerLayer?.removeFromSuperlayer()
        player?.pause()

    }

单元格占据了整个屏幕,所以当我想上下切换视频时,我想暂停上一个视频。

但是现在当您切换时,它将继续播放上一个视频

控制台出现:

2020-08-26 10:11:51.032419+0800 Demo[3253:76257] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600002234be0> F8BB1C28-BAE8-11D6-9C31-00039315CD46

非常感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...