I read the following article:
I am loading videos in AVPlayer in collection view but it repeats some cells data
My attempts:
controller:
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, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
cell:
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()
}
The cell takes up the entire screen, so when I want to switch videos up and down, I want to pause the previous video.
But now when you switch, it continues to play the previous video
Console appears:
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
Thank you very much for your help