Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set AVPlayer AVPlayerItem buffer size?

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?

like image 925
xywwjf Avatar asked Dec 18 '22 07:12

xywwjf


2 Answers

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

like image 80
Shackleford Avatar answered Jan 06 '23 16:01

Shackleford


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.

like image 34
rorschach Avatar answered Jan 06 '23 16:01

rorschach