Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS AVPlayer slow rebuffering after connection loss

I'm building a music streaming app using AVPlayer. Everything is working fine, music keeps playing in the background, etc. My problem is when I simulate connection loss on the phone (switch cellular data off, wait for the stream to stop and then switch back) it will take about 2 mins for the app to fill the buffer even if there's a 3G network present (I can surf the web meanwhile).

Everything is working in simulator, but not on device. If I wait 2 mins I'll get a proper playbackLikelyToKeepUp notification, and from that on everything is fine, but that rebuffering should be just a few seconds. It's ok when I simulate low bandwidth by turning off bandwidth control on my router, but when I simulate carrier change by switching off cellular data and switch it back its that long.

I'm calling beginInterruption on "playbackBufferEmpty" and call endInterruption on "playbackLikelyToKeepUp". The main problem is "playbackLikelyToKeepUp" arrives way too late.

- (void)beginInterruption {
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
}

- (void)endInterruptionWithFlags:(NSUInteger)flags {
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
    self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    if(self.interrupted) [self play];
    self.interrupted = NO;
}

Any ideas? I've spent my whole day with this issue, googled everything but I'm stuck. Thanks in advance.

like image 772
Skrew Avatar asked Aug 17 '12 16:08

Skrew


1 Answers

I suffered the same situation months ago, my conclusion is "playbackLikelyToKeepUp is not reliable".

My solution is checked the buffered TimeRange manually.

FYI, I made a AVPlayer class for remote media playback - HysteriaPlayer. You can use it directly, or look up the .m file.

Hope this helps.

like image 51
saiday Avatar answered Sep 29 '22 19:09

saiday