2

Should it be possible to call beginReceivingRemoteControlEvents in background? Does anyone have experience with a similar situation?

So far, I have concluded that I can't change categories and keep using the remote controls while in background.

When I change between categories, such as AVAudioSessionCategoryPlayback or AVAudioSessionCategoryPlayAndRecord, the audio session is deactivated and I have to call beginReceivingRemoteControlEvents once again. When this is done in foreground, it works perfectly. When it's done in background, it seems the new beginReceivingRemoteControlEvents doesn't work.

Any help on how I could achieve such thing would be really appreciated.

27 de Abril
  • 117
  • 1
  • 11

1 Answers1

2

Instead of using beginReceivingRemoteControlEvents, you may want to use the newer MPRemoteCommandCenter. For example:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

or, if you prefer to use a method instead of a block:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

To stop:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

or:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

You'll need to add this to the includes area of your file:

@import MediaPlayer;
mahboudz
  • 39,196
  • 16
  • 97
  • 124