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:)
This method implicitly creates an
AVPlayerItem
object. You can get the player item usingcurrentItem
.
So we know that when you use the first way, something similar to the second way is happening under the hood - an AVPlayerItem
will 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 an AVAsset
using the URL you specified too, since an AVPlayerItem
is:
An object used to model the timing and presentation state of an asset played by the player.
The wording suggests that you can't have a AVPlayerItem
without a AVAsset
. Indeed, AVPlayerItem.asset
is a non-optional property. So you really need an AVAsset
to create an AVPlayerItem
. That, combined with the fact that AVPlayerItem.init(url:)
is a convenience initialiser, and init(asset:automaticallyLoadedAssetKeys:)
is the designated one, I am quite sure AVPlayerItem.init(url:)
creates an AVAsset
under the hood as well.
If you are wondering why AVPlayer.init(playerItem:)
and AVPlayerItem.init(asset:)
exist when all three ways do the same thing anyway, they are for when you just so happen to be working with AVPlayerItem
s when you want to create a AVPlayer
, and when you just so happen to be working with AVAsset
s when you want to create an AVPlayerItem
.