0

I'm making a iPhone music app that handles music playing & pausing and maintains its own play list. I'm trying to make the play/pause remote control functions (on earbuds and slide-up screen) work for the music playback in my app.

Here's the code I put in the initialization method related to the music player (which is an instance of MPMusicPlayerController, and I'm sure through debugging that this block of code is called), I'll talk about the problem I'm facing after code:

[self.musicController beginGeneratingPlaybackNotifications];

// listen to remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];    

if ([self canBecomeFirstResponder]) {
    NSLog(@"Can be first responder");
    [self becomeFirstResponder];
    NSLog([NSString stringWithFormat: @"is first responder now? %@", [self isFirstResponder] ? @"yes" : @"no"]);
} else {
    NSLog(@"Cannot be first responder");
}

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: toggle command");
    return nil;
}];

[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: play command");
    return nil;
}];

[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
    NSLog(@"TK: pause command");
    return nil;
}];

My problem: after adding above code, when I press the play/pause button on the slide-up screen when the music is playing in my app, no debug message shows up in the log, meaning the event handler block wasn't triggered.

And when I press the play/pause button on my apple earbuds when the music is playing in my app, the system starts to play a random music in my iOS Music Library, and the song it's playing does not appear in the play list of my app, meaning that there's no legal way to play this song from my app except by doing above steps.

Note: the log has an output of "Cannot be first responder".

Ascendant
  • 2,430
  • 3
  • 26
  • 34

1 Answers1

0

You should enable view (view controller) to become first responder

- (BOOL)canBecomeFirstResponder { return true; }

DeVladinci
  • 466
  • 4
  • 14