Ios remoteControlReceivedWithEvent在模拟器中工作,但在设备中不工作

Ios remoteControlReceivedWithEvent在模拟器中工作,但在设备中不工作,ios,ios8,uiresponder,remote-control,mpmediaplayercontroller,Ios,Ios8,Uiresponder,Remote Control,Mpmediaplayercontroller,我正在开发一个应用程序,可以播放iPod库中的音乐。我正在通过MPMediaPlayerController播放音乐,方法是从表中检索所选项目并将其传递给局部视图控制器: MPMediaItem *item = (MPMediaItem *)self.detailItem; MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:@[item]]; [self.musicPlayer set

我正在开发一个应用程序,可以播放iPod库中的音乐。我正在通过MPMediaPlayerController播放音乐,方法是从表中检索所选项目并将其传递给局部视图控制器:

MPMediaItem *item = (MPMediaItem *)self.detailItem;
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:@[item]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];
开始播放音乐。我已在我的Info.plist中设置以下值以启用后台使用:

UIBackgroundModes
 >Item 0 - audio
这就行了。当我关闭应用程序时,音乐会一直播放。因此,现在我正试图让控制中心的音频控件向我的应用程序发送消息,因此在阅读了一些内容后,我发现我需要做一些事情。因此,我创建了UIResponder的子类,并添加了以下行:

- (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];
}
现在,我之所以在这里,是因为它在模拟器中工作,而不是在设备中,我不知道为什么。如果我在模拟器中启动它,打开控制中心并开始按音频控件,则自定义UIResponder中的NSLog会在调试器中显示,但在设备上不会显示。实际上,播放/暂停按钮不起任何作用,如果我按下下一个或上一个按钮,它会转到iPod应用程序上的下一个或上一个曲目并开始播放


这个等式似乎少了一些小东西,但我无法理解。我已经尽可能地搜索了文档,但找不到与此情况相关的任何内容,而且关于此特定功能的文档似乎非常有限。

在您的rootViewController中:

- (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;
        }
    }
}

用Danilo的答案在
viewDidLoad()
中尝试此代码

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

我知道这个答案很古老,但我在iOS 10中也遇到了同样的问题。我的模拟器正确显示了锁屏音频控制,但实际设备没有。对我来说,修复方法是确保在设置AVAudioSession的类别时,我没有分配选项:AVAudioSessionCategoryOptionMixWithOthers

这足以使我的设备不显示锁屏音频控件。因此,最终我的解决方案如下:

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

谢谢你的回复。我从其他地方删除了所有用于此的代码,并像您所说的那样将其放在我的rootViewController中。尽管如此,我还是可以在模拟器中通过拉起控制中心并按下play/pause next/previous(播放/暂停下一个/上一个)来实现这一点,我可以看到NSLog打印出来,但当我在设备上启动它时,它对我的应用程序不做任何操作,并尝试控制播放音乐的最后一个应用程序。还有什么我可能遗漏的吗?在viewDidLoad中,尝试在[super viewDidLoad]
AVAudioSession*audioSession=[AVAudioSession sharedInstance]之后立即添加此代码;n错误*setCategoryError=nil;BOOL success=[audioSession setCategory:AVAudioSessionCategoryPlayback with options:AVAudioSessionCategoryOptionDefaultToSpeaker错误:&setCategoryError];if(success){NSError*activationError=nil;success=[audioSession setActive:YES错误:&activationError];if(!success){[self-trackLog:@“Radio无可用的audioSession!”错误:activationError];}
在viewDidLoad NSError*SetCategorYer=nil中尝试此操作;N错误*激活器R=无;[[AVAudioSession sharedInstance]设置类别:AVAudioSessionCategoryPlayback with options:0错误:&SetCategorYer];[[AVAudioSession sharedInstance]设置活动:是错误:&激活器];