RemoteControlReceivedWithEvent works in the simulator, but not in the device

I am working on an application that will play music from the iPod library. I play music through MPMediaPlayerController, extracting the selected item from the table and passing it to the detail view controller:

MPMediaItem *item = (MPMediaItem *)self.detailItem;
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:@[item]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];

Start playing music. I set the following values ​​in my Info.plist to enable background usage:

UIBackgroundModes
 >Item 0 - audio

And it works. When I close the application, the music continues to play. So now I'm trying to get the audio controls in the control center to send messages to my application, so after some reading, I found that I need to do a few things. So I created a subclass of UIResponder and added the following lines:

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    NSLog(@"CustomApp:remoteControlReceivedWithEvent:%@", event.description);
}

AppDelegate UIResponder, :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.window = [[MainWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.mainViewController = [[BrowserViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
    self.window.rootViewController = navigationController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

, , , , , , . , NSLog UIResponder , . , , / , , , iPod .

, - , . , , , .

+4
3

, , iOS 10. , . , AVAudioSession , : AVAudioSessionCategoryOptionMixWithOthers.

, . :

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowAirPlay|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:&error];
+1

:

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidAppear:animated];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&setCategoryError];
    if (success) {
        NSError *activationError = nil;
        success = [audioSession setActive:YES error:&activationError];
        if (!success) {
            NSLog(@"%@", activationError);
        }
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        NSLog(@"Remote control event %i subtype %i", event.type, event.subtype);

        // example for headphones
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                break;
            case UIEventSubtypeRemoteControlPause:
                break;
            case UIEventSubtypeRemoteControlStop:
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                break;
            case UIEventSubtypeRemoteControlEndSeekingBackward:
                break;
            case UIEventSubtypeRemoteControlEndSeekingForward:
                break;

            default:
                break;
        }
    }
}
0

Try this code in viewDidLoad () with Danilo's answer

- (void)viewDidLoad:(BOOL)animated
{
        NSError *setCategoryErr = nil;
        NSError *activationErr  = nil;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:&setCategoryErr];
        [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];

}
0
source

All Articles