无法在iOS中开始接收远程控制事件

时间:2012-04-23 06:59:12

标签: objective-c ios background avaudioplayer remote-control

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

我像这样设置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

为什么会发生这种情况?

这就解决了我的问题Entering background on iOS4 to play audio

4 个答案:

答案 0 :(得分:13)

我在我的项目中做了同样的工作,它工作正常。请按照这个,也许它会帮助你。更改事件名称等。在我的代码中_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;
    }
}

答案 1 :(得分:4)

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

    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;

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

答案 2 :(得分:1)

从Apple查看此sample code以获取使用示例。可能你的主视图控制器实际上并没有成为第一个响应者。另一种用法是将此代码放在您的应用程序代理(它始终是第一个响应者)中,并在它们有机会传播之前响应这些事件,并可能被其他响应者使用。

答案 3 :(得分:0)

你有

respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){

但是在方法签名中你有

- (void)remoteControlReceivedWithEvent:(UIEvent *)event

您注册签名的地方是错误的。只需用

替换它
respondsToSelector:@selector(beginReceivingRemoteControlEvents:)]){

你错过了:这使得应用程序将偶数发送到我假设的非现有方法。我很惊讶你的应用程序没有崩溃。