does automaticallyLoadedAssetKeys actually work?

57 Views Asked by At

I am wondering whether AVPlayerItem's initialization with automaticallyLoadedAssetKeys actually works. Or do I have a wrong logic to check this?

Below is my test code.

let url = ...
let asset = AVAsset(url: url)
let item = AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: ["duration"])

// try explicitly loading the duration property
// it always takes less than 2~3 seconds
Task {
    do {
        let duration = try await asset.load(.duration)
    }
    ...
}

// without above task, it always prints "Not yet loaded"
// with it, it successfully prints "Loaded"

DispatchQueue.main.asyncAfter(deadline: .now() + 30.0) {
    print("30 seconds after: ", asset.status(of: .duration))
}

I assumed that the properties specified in automaticallyLoadedAssetKeys are possible to check through asset.status. What's wrong here? Many thanks in advance.


EDIT #1

When I call avPlayer.replaceCurrentItem(with: item), then logs are correctly printed after the 30 seconds. Then what's the point of automaticallyLoadedAssetKeys? I would like to preload tracks or other necessary keys to reduce the initial startup time of playback when actually plays the video later.

0

There are 0 best solutions below