I'm trying to show 5 different videos in five different UICollectionView cells. It sounds pretty easy to do and with 3 cells it was working fine but now somehow all the videos are in the wrong cells.
It's an ugly solution but it was working before and I can't figure out any other solution.
Here's five different instances for AVPlayer and AVPlayerLayer.
let theURL = Bundle.main.url(forResource:"summer", withExtension: "mp4")
let theURL2 = Bundle.main.url(forResource:"newyork", withExtension: "mp4")
let theURL3 = Bundle.main.url(forResource:"amber", withExtension: "mp4")
let theURL4 = Bundle.main.url(forResource:"luckywhite", withExtension: "mp4")
let theURL5 = Bundle.main.url(forResource:"coconut", withExtension: "mp4")
avPlayer = AVPlayer(url: theURL!)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer.volume = 0
avPlayer.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer.currentItem)
avPlayer2 = AVPlayer(url: theURL2!)
avPlayerLayer2 = AVPlayerLayer(player: avPlayer2)
avPlayerLayer2.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer2.volume = 0
avPlayer2.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer2.currentItem)
avPlayer3 = AVPlayer(url: theURL3!)
avPlayerLayer3 = AVPlayerLayer(player: avPlayer3)
avPlayerLayer3.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer3.volume = 0
avPlayer3.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer3.currentItem)
avPlayer4 = AVPlayer(url: theURL4!)
avPlayerLayer4 = AVPlayerLayer(player: avPlayer4)
avPlayerLayer4.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer4.volume = 0
avPlayer4.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer4.currentItem)
avPlayer5 = AVPlayer(url: theURL5!)
avPlayerLayer5 = AVPlayerLayer(player: avPlayer5)
avPlayerLayer5.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer5.volume = 0
avPlayer5.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer5.currentItem)
And here I put the videos into the cells.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
switch indexPath.item {
case 0:
self.avPlayerLayer.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer, at: 0)
case 1:
self.avPlayerLayer2.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer2, at: 0)
case 2:
self.avPlayerLayer3.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer3, at: 0)
case 3:
self.avPlayerLayer4.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer4, at: 0)
default:
self.avPlayerLayer5.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer5, at: 0)
}
Why it's not working properly? I think I'm doing everything right when playing with indexPaths.
Cells are reused and when you do
self.avPlayerLayer4.frame = cell.image.layer.bounds
cell.image.layer.insertSublayer(self.avPlayerLayer4, at: 0)
so consider cleaning it before the insert
cell.image.layer.sublayers?.forEach {
if $0 is AVPlayerLayer {
$0.removeFromSuperlayer()
}
}
it adds the layer at the most back and show the old added layer before , instead of duplicated code you need to write a func that return a layer from a url and add it
Aslo consider having an array like
var arr = [URL]()
and use it instead of the switch
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.