I found the three ways to play video with URL.
let url = "some url"
// first way
AVPlayer(url: url)
// second way
let playerItem = AVPlayerItem(url: url)
AVPlayer(playerItem: playerItem)
// third way
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
AVPlayer(playerItem: playerItem)
Is there any difference between above these?
From the docs of
AVPlayer.init(url:)So we know that when you use the first way, something similar to the second way is happening under the hood - an
AVPlayerItemwill be created with the URL you specified. Therefore, the first and second way are the same.Although the docs doesn't say this explicitly, I'm pretty sure
AVPlayerItem.init(url:)creates anAVAssetusing the URL you specified too, since anAVPlayerItemis:The wording suggests that you can't have a
AVPlayerItemwithout aAVAsset. Indeed,AVPlayerItem.assetis a non-optional property. So you really need anAVAssetto create anAVPlayerItem. That, combined with the fact thatAVPlayerItem.init(url:)is a convenience initialiser, andinit(asset:automaticallyLoadedAssetKeys:)is the designated one, I am quite sureAVPlayerItem.init(url:)creates anAVAssetunder the hood as well.If you are wondering why
AVPlayer.init(playerItem:)andAVPlayerItem.init(asset:)exist when all three ways do the same thing anyway, they are for when you just so happen to be working withAVPlayerItems when you want to create aAVPlayer, and when you just so happen to be working withAVAssets when you want to create anAVPlayerItem.