【问题标题】:Can't beginReceivingRemoteControlEvents in iOS无法在 iOS 中开始接收远程控制事件
【发布时间】:2012-04-23 06:59:12
【问题描述】:

在我的应用程序中,我想让用户在后台控制音频播放。我在 .plist 中设置了背景模式,并在 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

【问题讨论】:

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


    【解决方案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;
        }
    }
    

    【讨论】:

    • 谢谢,但据我所知,是一样的
    • 另外一件事是你不能在模拟器上测试,如果你使用模拟器来测试移除事件和背景音乐,请使用设备。
    • 一旦我添加了 -(BOOL)canBecomeFirstResponder{return YES;},这对我有用。谢谢!
    • 亲爱的@openfrog,如果您知道它在 ios 6+ 中的工作原理,那么您应该更改它或使用标签“IOS 6+”做出新的答案,而不是对此投反对票,因为仍然有世界上使用 ios 5 和 4 的人。
    【解决方案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;
    

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        你有

        respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        

        但是在你的方法签名中

        - (void)remoteControlReceivedWithEvent:(UIEvent *)event
        

        你注册签名的地方不对。换成

        respondsToSelector:@selector(beginReceivingRemoteControlEvents:)]){
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-08
          • 2015-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多