-->

iOS中无法beginReceivingRemoteControlEvents(Can't

2019-07-29 03:09发布

在我的应用程序,我想,让用户来控制后台音频播放。 我的.plist设置背景模式,并在BG戏就像我wanted.But我不能得到触摸控制按钮任何回应。

我设置了AudioSession这样

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];

然后,在viewContoller在我的球员放在我beginReceivingRemoteControlEvents这样

 if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}

而且它打印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

为什么能这样呢?

这里的解决方案就是解决我的问题上的iOS4进入后台播放音频

Answer 1:

我也做了同样的工作在我的项目,它的正常工作。 请点击此,或许它会帮助你。 更改事件名称等。在我的代码_audio是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;
    }
}


Answer 2:

有听远程控制事件更新的机制。 例如,当按下耳机播放/暂停按钮时执行的块:

    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];

要么:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

你需要把它添加到您的文件包括区域:

@import MediaPlayer;

它在后台工作,你必须添加到您的应用程序的功能,后台音频模式。



Answer 3:

查看此示例代码从苹果公司使用的一个例子。 可能您的主视图控制器实际上没有成为第一个响应者。 另一种用途是用来放置在你的应用程序委托该代码(这将永远是第一个响应者),他们有机会来传播,并有可能被其他反应消耗之前,这些事件作出回应。



Answer 4:

你有

respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){

但在方法签名你有

- (void)remoteControlReceivedWithEvent:(UIEvent *)event

你在哪里注册签字的地点是错误的。 仅仅通过更换

respondsToSelector:@selector(beginReceivingRemoteControlEvents:)]){

这是因为丢失了:这是使应用程序发送的甚至不存在的方法,我假设。 我很惊讶,你的应用程序没有崩溃。



文章来源: Can't beginReceivingRemoteControlEvents in iOS