iOS控制中心音乐控制在iOS 11更新中停止工作(remoteControlReceivedWithEvent不称为iOS 11)

问题描述:

我遇到了iOS控制中心音乐控件的问题在iOS 11更新之前,Play Pause按钮已启用并正常工作,如预期的那样。iOS控制中心音乐控制在iOS 11更新中停止工作(remoteControlReceivedWithEvent不称为iOS 11)

但是,在iOS 11中它停止工作。研究之后,我发现,在IOS 11 remoteControlReceivedWithEvent永远不会被调用,而是,在旧的IOS版本例如iOS 9它被通常称为

设置我的事件上AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Enable background audio listening 
    // https://developer.apple.com/library/ios/qa/qa1668/_index.html 
    NSError *error = nil; 
    if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]) { 
     NSLog(@"%@", error); 
    } 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
} 


- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent { 
    if (receivedEvent.type == UIEventTypeRemoteControl) { 
     switch (receivedEvent.subtype) { 
      case UIEventSubtypeRemoteControlPlay: 
       [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil]; 
       break; 
      case UIEventSubtypeRemoteControlPause: 
       [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil]; 
       break; 
      case UIEventSubtypeRemoteControlTogglePlayPause: 
       [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil]; 
       break; 
      default: 
       break; 
     } 
    } 
} 

也我订阅了远程事件在另一个类控制播放/暂停按钮

- (void)subscribeToRemoteControlEvents { 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) { 
     // Disables the forward/backward buttons and only shows the play button. 
     // You can't just enable the command, you must subscribe for this to activate, so 
     // the subscription in this case doesn't do anything. 
     [MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand.enabled = YES; 
     [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(ignore_removeCommandCenterFired)]; 
    } 

    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePlayPauseNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) { 
     if (observer.isCastPlaying) { 
      [observer pause]; 
     } 
     else { 
      [observer play:NO]; 
     } 
    }]; 
} 

- (void)unsubscribeFromRemoteControlEvents { 
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) { 
     [[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand removeTarget:self action:@selector(ignore_removeCommandCenterFired)]; 
    } 
} 

我想知道为什么不工作了我也为您在文档中的API的变化,但我没有看到变化

注:我查看以下链接,没有运气

iOS - UIEventTypeRemoteControl events not received

https://forums.developer.apple.com/thread/84204

Unable to receive remoteControlReceivedWithEvent - objective c - ios

remoteControlReceivedWithEvent in AVAudio is not being called

remoteControlReceivedWithEvent not Called in appDelegate

最后,我用remoteCommandCenter解决这个问题,并播放和暂停按钮代替tooglePlayPauseCommand

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")){ 
    //NOTE: this is the only way that I find to make this work on IOS 11 its seems to be that togglePlayPauseCommand is not working anymore 
    MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; 
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayNotificationName object:nil]; 
     return MPRemoteCommandHandlerStatusSuccess; 
    }]; 

    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePauseNotificationName object:nil]; 
     return MPRemoteCommandHandlerStatusSuccess; 
    }]; 

    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePlayNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) { 
     if (!observer.isCastPlaying) { 
      [observer play:NO]; 
     } 
    }]; 

    [[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePauseNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) { 
     if (observer.isCastPlaying) { 
      [observer pause]; 
     } 
    }]; 


} 

就由胡安的回答修改。

if(@available(iOS 11, *)) { 
    MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; 
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"RemotePlayCommandNotification" object:nil]; 
     return MPRemoteCommandHandlerStatusSuccess; 
    }]; 

    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"RemotePauseCommandNotification" object:nil]; 
     return MPRemoteCommandHandlerStatusSuccess; 
    }]; 

    [[NSNotificationCenter defaultCenter] addObserverForName:@"RemotePlayCommandNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { 

     NSLog(@"Clicked the play button."); 
    }]; 

    [[NSNotificationCenter defaultCenter] addObserverForName:@"RemotePauseCommandNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { 

     NSLog(@"Clicked the pause button."); 
    }]; 

}