4
votes

Playing video with avplayer, listening to the loadedTimeRanges property, playing about 10 minutes of video, avplayer always preload the video, and feel very costly, is there a way to limit the size of the preloaded area? Such as pre-loading video half of the time?

2

2 Answers

8
votes

I think you're looking for AVPlayerItem's preferredForwardBufferedDuration property.

Per Apple:

This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.

See https://developer.apple.com/reference/avfoundation/avplayeritem/1643630-preferredforwardbufferduration?language=objc

3
votes

If you use resourceLoaderdelegate you can control the exact amount of content that gets preloaded/downloaded prior to playback.

The code example here is a good start - AVPlayer stalling on large video files using resource loader delegate

Basically, you would have to maintain an array of pendingRequests and process them once by one by firing up URLSession dataTask until you have downloaded enough content that you would like to preload.

Cheers.