问题
In a WWDC 2016 video, Apple says create the AVPlayer without an AVPlayerItem, create the AVPlayerLayer, and then assign the AVPlayerItem to the AVPlayer.
I got the above code from @matt's Programming iOS 10 book. In it he says:
Apparently there is some sort of efficiency if you do things in this order. The reason, it turns out, is that when an AVPlayerItem is assigned to an AVPlayer that doesn't have and associated AVPlayerLayer, the AVPlayer assumes that only the audio track of the AVAssest is important -and then, when an AVPlayerLayer is assigned, it must scramble to pick up the video tracks as well
Even though that was iOS 10, does that efficiency still stand for iOS 13?
do it this way:
let asset = AVAsset(url: url)
let item = AVPlayerItem(asset: asset)
let player = AVPlayer()
let layer = AVPlayerLayer(player: player)
player.replaceCurrentItem(with: item) // playerItem set here
don't do it this way:
let asset = AVAsset(url: url)
let item = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: item) // playerItem set here
let layer = AVPlayerLayer(player: player)
来源:https://stackoverflow.com/questions/62489214/does-setting-the-avplayeritem-after-creating-the-avplayer-increase-efficiency-in