8

I'm using an AVPlayer object to play a remote radio stream in my iOS app. The stream works fine and plays in the background.

Doing some connection tests I've come across some issues. Once the player connection is lost the player stops (as you'd expect) however i cannot make the player begin again when the connect is back.

I've crudely setup a timer to just hit [player play] every second to force it to start but no luck. My best guess is based on the lack of data being requested it's just died.

I do have an observer setup to monitor when the player is ready to start or if there is an error but seemingly this isn't being called at all after the initial time.

My question is how to i force the AVPlayer to begin getting the stream again when it's available.

I've subclassed 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;

}
slaterjohn
  • 249
  • 2
  • 9
  • 1
    we're having same issue. Looks like bug in AVPlayer, per http://stackoverflow.com/questions/28059019/avplayer-cannot-resume-upon-wifi-drops-in-ios-8 – Andy Jun 10 '15 at 00:38

0 Answers0