Ios 音频遥控器';行不通

Ios 音频遥控器';行不通,ios,events,audio,controls,mpmovieplayercontroller,Ios,Events,Audio,Controls,Mpmovieplayercontroller,之后,我设法用MPMoviePlayerController在后台播放音频,并试图让它接受远程控制。但当我点击播放/暂停按钮时,没有反应,音频继续播放。 然后我试图显示是否有日志输出,但没有输出 这是我的密码: -(void)viewDidAppear:(BOOL)animated{ ... [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFi

之后,我设法用MPMoviePlayerController在后台播放音频,并试图让它接受远程控制。但当我点击播放/暂停按钮时,没有反应,音频继续播放。 然后我试图显示是否有日志输出,但没有输出

这是我的密码:

   -(void)viewDidAppear:(BOOL)animated{
...
       [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
        [self resignFirstResponder];
    }

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
        NSLog(@"remoteControlReceivedWithEvent: %@", event);
        if (event.type==UIEventTypeRemoteControl) {
            if (event.subtype==UIEventSubtypeRemoteControlPlay) {
                NSLog(@"Play");
            } else if (event.subtype==UIEventSubtypeRemoteControlPause) {
                NSLog(@"Pause");
            } else if (event.subtype==UIEventSubtypeRemoteControlTogglePlayPause) {
                NSLog(@"Play Pause");
            }
        }
    }

感谢您在advanced中的努力。

看起来您的视图控制器不在响应程序链中。
这可能是由于视图中的[self-resignFirstResponder]将消失和/或另一个ViewController位于前面

为了确保接收到这些事件,您可以实现一个UIApplication子类,该子类将能够始终接收事件。因此,哪个ViewController可见并不重要。 UIApplication始终位于响应程序链的末尾

为UIApplication(MyUIApplication)创建子类并实现以下UIResponder方法:

- (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    ...
}

- (BOOL)canBecomeFirstResponder {
  return YES;
}
在main.m中,使用以下命令启动应用程序(用类名替换MyUIApplication和AppDelegate):

@autoreleasepool {
      return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
}