remoteControlReceivedWithEvent未在appDelegate中调用

Jan*_*nub 21 iphone ios

我有问题用我的avplayer控制iPhone控件.

如果我把功能

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

在负责播放被调用函数的视图控制器中但仅当我要在当前视图控制器中进行后台操作时.

如果我要去其他视图控制器的背景,那么函数永远不会调用.

这就是为什么我想把它放在app委托中.

我尝试了Becomefirstresponse并将该函数放在每个视图控制器中但它确实有帮助.

我打电话

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)

在里面

-(void)applicationWillResignActive:(UIApplication *)application
Run Code Online (Sandbox Code Playgroud)

谢谢

San*_*ndy 23

我在iPhone控件中使用了以下代码 -

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
Run Code Online (Sandbox Code Playgroud)

用于注册监听遥控器.完成后删除它 -

[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
Run Code Online (Sandbox Code Playgroud)

让app canBecomeFirstResponder -

- (BOOL)canBecomeFirstResponder {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

使用委托方法处理iPhone控件,如播放和暂停,同时点击主页按钮

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl) {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
           [audioPlayer play];
            NSLog(@"play");
        } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
           [audioPlayer stop];
             NSLog(@"pause");
        } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
            NSLog(@"toggle");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的情况下,我能够处理播放和暂停.请让我知道是否有任何错误.


Far*_*ler 12

您可以将响应程序链中的函数移动到UIApplication子类.这样,它总能在那里捕捉事件.

在常见的UI和控制器类中忽略了这种事件,因此它会一直传到应答程序链的底部,即应用程序委托和应用程序本身所在的位置.

如前所述这里,UIApplication的的委托不响应链的一部分(我错了这里).UIApplication就在那里,root UIWidow也是如此,链中的所有视图和相应的UIViewControllers都是如此.