Question

I'm writing an app that streams music and I'm having a ton of trouble setting the icon on the music control dock screen (when you double-click the home button and swipe to the left). All the documentation said to do something like this, but the icon never appears and I don't ever receive the event notification. All of this code is in my player view controller. _radioPlayer is an instance of an AVQueuePlayer. What am I doing wrong here?

- (void)viewWillAppear:(BOOL)animated
{
    UIApplication *application = [UIApplication sharedApplication];
    if([application respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
        [application beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            [_radioPlayer pause];
            break;
        case UIEventSubtypeRemoteControlPlay:
            [_radioPlayer play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [_radioPlayer pause];
            break;
        default:
            break;
    }
}

EDIT: I read through the documentation and followed the steps but it's still not working. Here are some of the possible reasons I've come up with:

A. My music isn't playing yet when viewDidAppear is called. In the documentation it states

"Your app must be the “Now Playing” app. Restated, even if your app is the first responder >and you have turned on event delivery, your app does not receive remote control events until >it begins playing audio."

I tried calling beginReceivingRemoteControlEvents: and becomeFirstResponder: after the music starts playing but this doesn't work either. Can these only be called in viewDidAppear:? Does iOS automatically detect when the music begins playing so this isn't necessary?

B. There is something weird about using an AVQueuePlayer. I'm almost positive this isn't it since the event message is handled by the view controller, not the player itself.

Was it helpful?

Solution

It may not working for you because you are calling beginReceivingRemoteControlEvents in viewWillAppear instead of viewDidAppear. Check out the documentaion -

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    // Turn on remote control event delivery

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top