在UICollectionViewCell中播放视频。视频播放正常但在collectionView上具有滚动性能

时间:2017-01-24 12:43:37

标签: ios swift uicollectionview avplayer avplayeritem

我在社区上探索了其他答案并试了一下 -

    player = AVPlayer()
    player.volume = 0.0
    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
    addPlayerLayer()

    let asset = AVAsset.init(URL: videoURL)
    asset.loadValuesAsynchronouslyForKeys(["duration", "playable"]) {

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let item = AVPlayerItem.init(asset: asset)
                self.player.replaceCurrentItemWithPlayerItem(item)
                self.player.play()
        })
    }

但是只要网络速度慢,滚动collectionView时就会出现明显的滞后/打嗝。难道我做错了什么?请帮忙

1 个答案:

答案 0 :(得分:2)

请查看Facebook的AsyncDisplayKit(Facebook和Instagram Feed背后的引擎)您可以使用他们的AVideoNode在后台线程上渲染视频。

OR:

player = AVPlayer()
player.volume = 0.0
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
addPlayerLayer()
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {}
    let asset = AVAsset.init(URL: videoURL)
    asset.loadValuesAsynchronouslyForKeys(["duration", "playable"]) {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let item = AVPlayerItem.init(asset: asset)
                self.player.replaceCurrentItemWithPlayerItem(item)
                self.player.play()
        })
    }
})