2017-10-18 99 views
0

我有一個適用於Apple TV的UICollectionView。最多6個單元全部顯示在同一個屏幕上。每個單元格都有不同的循環播放視頻。但是,我發現不止兩個視頻導致非常波濤洶涌的視頻播放。如何提高播放視頻的性能?UICollectionView視頻性能

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 
} 

回答

0

問題是由源視頻這是1920X1080的分辨率造成的調用。降低分辨率解決了問題。