set AVPlayer AVPlayerItem buffer size?
Asked Answered
C

2

7

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?

Coricoriaceous answered 1/5, 2017 at 13:44 Comment(0)
P
9

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

Psychosis answered 1/5, 2017 at 17:46 Comment(1)
Thank you, but this property may not be I am looking for, I use avplayer, but it is automatically to buffer the video, when using KVO monitor loadedTimeRanges attribute, found that even if I do not read all the video, it will all loaded, I think Is there a way to load only a portion of the cache.Coricoriaceous
B
3

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.

Berzelius answered 30/7, 2018 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.