I want to reconnect to the server when the streaming buffer is empty.
How can I trigger a method when the AVPlayer
or AVPlayerItem
buffer is empty?
I know there are playbackLikelyToKeepUp
, playbackBufferEmpty
and playbackBufferFull
methods to check the buffer status, but those are not callbacks.
Are there any callback functions, or any observers I should add?
Try this code it should solve all your nightmares:
You will need to drop down into Core Audio and CFReadStream to do this. WIth CFReadStream, you can provide a callback that gets called on certain stream events like end encountered, read error, etc. From there you can trigger a reconnect to the server. If you're consuming an HTTP stream, you can add the range header to the HTTP request so you can tell the server to send the stream from a point you specify (which would be the last byte you received before + 1).
you can add observer for those keys:
The first one will warn you when your buffer is empty and the second when your buffer is good to go again.
Then to handle the key change you can use this code: