I’ve a set view and in assortment view cell I add video to play I need that just one video play at a time and it working completely however after I see standing of my cpu in xcode I’ve an important downside right here
As you realize assortment view and desk view have cell reuse downside so each time I scroll my movies my cpu’s standing go upto 200+. Which Shouldn’t be good for well being of mobiles
I come from logic that say’s I add my play and pause logic to my button motion however I do not know tips on how to add the play and pause logic in my button as a result of I’ve a button in cells xib file so I desire a resolution for cpu’s excessive load can anybody please assist me
I’ve this code for my assortment views cell :- import UIKit
import AVFoundation
class VideoCollectionViewCell: UICollectionViewCell {
var participant: AVPlayer?
var playerLayer: AVPlayerLayer?
var playPauseButton: UIButton!
override func awakeFromNib() {
tremendous.awakeFromNib()
// Setup participant layer
playerLayer = AVPlayerLayer()
playerLayer?.videoGravity = .resizeAspect
contentView.layer.addSublayer(playerLayer!)
// Setup play/pause button
playPauseButton = UIButton(kind: .system)
playPauseButton.setTitle("Play", for: .regular)
playPauseButton.addTarget(self, motion: #selector(playPauseTapped), for: .touchUpInside)
contentView.addSubview(playPauseButton)
// Structure button
playPauseButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
playPauseButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
playPauseButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
playPauseButton.widthAnchor.constraint(equalToConstant: 100),
playPauseButton.heightAnchor.constraint(equalToConstant: 50)
])
}
override func layoutSubviews() {
tremendous.layoutSubviews()
playerLayer?.body = contentView.bounds
}
func configure(with url: URL) {
participant = AVPlayer(url: url)
playerLayer?.participant = participant
}
@objc func playPauseTapped() {
if participant?.timeControlStatus == .enjoying {
pause()
} else {
play()
}
}
func play() {
participant?.play()
playPauseButton.setTitle("Pause", for: .regular)
}
func pause() {
participant?.pause()
playPauseButton.setTitle("Play", for: .regular)
}
override func prepareForReuse() {
tremendous.prepareForReuse()
participant?.pause()
playerLayer?.participant = nil
}
}
And right here the of view controller
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var videoURLs: [URL] = [/* Your video URLs here */]
var currentlyPlayingIndex: IndexPath?
override func viewDidLoad() {
tremendous.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "VideoCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "VideoCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return videoURLs.depend
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! VideoCollectionViewCell
cell.configure(with: videoURLs[indexPath.item])
if indexPath == currentlyPlayingIndex {
cell.play()
} else {
cell.pause()
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let currentIndex = currentlyPlayingIndex, let currentCell = collectionView.cellForItem(at: currentIndex) as? VideoCollectionViewCell {
currentCell.pause()
}
let cell = collectionView.cellForItem(at: indexPath) as! VideoCollectionViewCell
cell.play()
currentlyPlayingIndex = indexPath
}
// Making certain that the video continues enjoying even when the cell goes off-screen
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let currentlyPlayingIndex = currentlyPlayingIndex else { return }
if let cell = collectionView.cellForItem(at: currentlyPlayingIndex) as? VideoCollectionViewCell {
// Make sure the cell retains enjoying the video if it is the at present enjoying index
cell.play()
}
}
}