gpt4 book ai didi

swift - UICollectionView 视频性能

转载 作者:行者123 更新时间:2023-11-30 12:04:43 24 4
gpt4 key购买 nike

我有一个适用于 Apple TV 的 UICollectionView。同屏最多显示 6 个单元格。每个单元格都有不同的循环视频播放。但是,我发现超过 2 个视频会导致视频播放非常断断续续。如何提高视频播放的性能?

class ExerciseCell: UICollectionViewCell {

@IBOutlet private weak var stationNumberLabel: UILabel!
@IBOutlet private weak var exerciseNameLabel: UILabel!

private var looperPlayer:AVPlayerLooper!

var exercise:ExerciseData! {
didSet {
self.stationNumberLabel.text = exercise.stationNumber
self.exerciseNameLabel.text = exercise.exerciseName
playVideo()
}
}

private func playVideo()
{
//if let videoURL = URL(string:self.exercise.videoURL)
if let videoPath = Bundle.main.path(forResource: self.exercise.videoName, ofType: "mp4")
{
//let playerItem = AVPlayerItem(url: videoURL)
let playerItem = AVPlayerItem(url: URL(fileURLWithPath: videoPath))
let queuePlayer = AVQueuePlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: queuePlayer)

playerLayer.frame = self.layer.bounds
playerLayer.videoGravity = .resize
playerLayer.player = queuePlayer

self.looperPlayer = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
self.layer.insertSublayer(playerLayer, at: 0)

queuePlayer.volume = 0.0
queuePlayer.play()
}
else
{
self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
}
}}

上面是从UICollectionViewController调用的

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ExerciseCell

cell.exercise = self.exercisesArray[indexPath.row]

return cell
}

最佳答案

该问题是由源视频的分辨率为 1920X1080 引起的。降低分辨率解决了问题。

关于swift - UICollectionView 视频性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46802928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com