I am using an AVPlayer object to play a remote radio stream in an iOS application. The stream works fine and plays in the background.
While performing some connection tests, I ran into some problems. As soon as the player’s connection is lost, the player stops (as expected), but I can’t get the player to start again when the connection returns.
I roughly set the timer to press [play player] every second to get it to start, but no luck. My best guess is based on the lack of requested data that he just died.
I have an observer setting for monitoring when a player is ready to start or if there is an error, but it does not seem to be called at all after the initial time.
My question is how to get AVPlayer to start receiving the stream again when it is available.
I have reclassified the audio player to BCRadio
-(BCRadio*)initWithRadioOneAndAutoPlay:(BOOL)autoPlay whenReady:(void(^)(void))ready whenFailed:(void(^)(void))failed whenBecameActive:(void(^)(void))active { // Current item self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://69.64.92.79:8276"]]; // Super it self = [super initWithPlayerItem:self.playerItem]; // Observe for it become ready [self addObserver:self forKeyPath:@"status" options:0 context:nil]; // If app comes from background and if app goes inactive [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecameActive) name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; // Set blocks self.playerReady = ready; self.playerFailed = failed; self.playerActive = active; self.willAutoPlay = autoPlay; // Register for remote notifications [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; // Play is pressed [[NSNotificationCenter defaultCenter] addObserverForName:@"TogglePlay" object:nil queue:NULL usingBlock:^(NSNotification *notification){ [self doPlay:YES]; }]; // Pause is pressed [[NSNotificationCenter defaultCenter] addObserverForName:@"TogglePause" object:nil queue:NULL usingBlock:^(NSNotification *notification){ [self doPlay:NO]; }]; // Check if playing NSTimer *timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer){ [self statusMonitor]; } repeats:YES]; [timer fire]; // Monitor reachability and pause when needed Reachability* reach = [Reachability reachabilityWithHostname:@"69.64.92.79"]; reach.reachableBlock = ^(Reachability*reach){ if(self.willAutoPlayOnResume && !self.playing){ self.willAutoPlayOnResume = NO; [self doPlay:YES]; } }; reach.unreachableBlock = ^(Reachability*reach){ if(self.playing){ self.willAutoPlayOnResume = YES; [self doPlay:NO]; } }; [reach startNotifier]; return self; }
source share