Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Can';t开始在iOS中接收RemoteControlEvents_Objective C_Ios_Background_Avaudioplayer_Remote Control - Fatal编程技术网

Objective c Can';t开始在iOS中接收RemoteControlEvents

Objective c Can';t开始在iOS中接收RemoteControlEvents,objective-c,ios,background,avaudioplayer,remote-control,Objective C,Ios,Background,Avaudioplayer,Remote Control,在我的应用程序中,我想让用户在后台控制音频播放。我在.plist中设置了背景模式,在bg中也设置了背景模式,就像我想要的那样。但是我无法通过触摸控制按钮得到任何响应 我像这样设置AudioSession [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance]setActive:YES error:nil];

在我的应用程序中,我想让用户在后台控制音频播放。我在.plist中设置了背景模式,在bg中也设置了背景模式,就像我想要的那样。但是我无法通过触摸控制按钮得到任何响应

我像这样设置
AudioSession

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}
然后,在放置播放机的viewContoller中,我开始接收RemoteControlEvents,如下所示

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}
它会打印
响应

但问题是这个方法永远不会被调用

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    NSLog(@"Where is my event?");
    if(event.type == UIEventTypeRemoteControl)
    {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"Pause");
                [self playWords:playButton];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                NSLog(@"Next");
                [self next];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                NSLog(@"Prev");
                [self prev];
                break;

        }
    }
我甚至试着在
UIApplication
上写一个分类,让它成为第一个响应者,但没有帮助

@implementation UIApplication (RemoteEvents)
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
@end
为什么会发生这种情况

解决方案
以下是解决我问题的方法

我在我的项目中也做了同样的工作,效果很好。请跟随这个,也许它会帮助你。更改事件名称等。在我的代码中,音频是AVAudioPlayer的对象

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

- (void)viewWillAppear {

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


- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            [_audio play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [_audio pause];
            break;
        default:
            break;
    }
}
查看Apple提供的这篇文章,了解用法示例。可能您的主视图控制器实际上并没有成为第一响应者。另一种用法是将此代码放在应用程序委托(始终是第一个响应者)中,并在这些事件有机会传播并可能被其他响应者使用之前对其进行响应。

respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
但是在方法签名中

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
您注册签名的位置是错误的。换成

respondsToSelector:@selector(beginReceivingRemoteControlEvents:)]){

您缺少了:这使得应用程序向我假设的不存在的方法发送偶数。我很惊讶你的应用程序没有崩溃。

有一种新的机制来监听远程控制事件。例如,要在按下耳机播放/暂停按钮时执行块:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
或者,如果您更喜欢使用方法而不是块:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];
停止:

    [commandCenter.togglePlayPauseCommand removeTarget:self];
或:

您需要将以下内容添加到文件的“包含”区域:

@import MediaPlayer;

要使其在后台工作,您必须将后台音频模式添加到应用程序的功能中。

谢谢,但据我所知,这是一样的。另一件事是,您不能在模拟器上测试它,如果您正在使用模拟器测试删除事件和背景音乐,请使用设备。我添加了-(BOOL)后,这对我很有效可以成为第一响应者{返回YES;}。谢谢亲爱的@openfrog,如果你知道它在ios 6+中是如何工作的,那么你应该改变它或者用标签“ios 6+”做出一个新的答案,而不是对此投反对票,因为世界上仍然有人在使用ios 5和4。