iOS - 未收到 UIEventTypeRemoteControl 事件

标签 ios events event-handling uiwindow

我在 AppDelegate (didFinishLaunching) 中有这个:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

我尝试在相关的 View Controller 中处理事件,但情况参差不齐(有些 View Controller 会获取事件,而其他人则不会,即使它们是第一响应者)。我尝试子类化 UIApplication。那没有用。现在我正在尝试继承 UIWindow 并执行此操作(请参阅评论):

- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
    NSLog(@"I wish this happened"); //this never happens
}
else
{
    NSLog(@"some other event"); //this does happen on any other interaction
    [super sendEvent:event];
}
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

if (receivedEvent.type == UIEventTypeRemoteControl) {
    NSLog(@"got remote event"); // this never happens either
    switch (receivedEvent.subtype) {

        case UIEventSubtypeRemoteControlTogglePlayPause:
        {
            NSLog(@"handle play/pause");
            break;
        }


        default:
            break;
    }
}
}

我已经在信息 plist 中尝试过使用和不使用它:

<key>UIBackgroundModes</key>
<array>
        <string>audio</string>
</array>

没有区别。正如 Apple 文档所述,您可以使用音频控件模拟远程控制事件(双击主页按钮并在底部滚动到它们)。当我按下播放或暂停时,它只会播放我的 iTunes 资料库中的音频。我也试过 Apple 耳机上的按钮。什么都没有。

我只想检测 Remote 上的播放/暂停按钮,并处理该事件。我还需要做什么才能捕捉到这些事件?

最佳答案

好的,我的生命中还有几个小时进入 Apple 的黑洞。事实证明,您只能在使用 AVAudioPlayer 播放某种音频或播放包含音频的视频文件后才能捕获远程控制事件。这在任何地方都没有记录here .

所以在我的例子中,我必须先播放一个虚拟的无声音频文件,然后再 try catch 远程控制事件。希望这对以后的人有所帮助。

(更新) 请注意 updated documentation现在声明如下:

To receive remote control events, your app must do three things:

  • Be the first responder. The view or view controller that presents the multimedia content must be the first responder.
  • Turn on the delivery of remote control events. Your app must explicitly request to begin receiving remote control events.
  • Begin playing audio. Your app must be the “Now Playing” app. Restated, even if your app is the first responder and you have turned on event delivery, your app does not receive remote control events until it begins playing audio.

关于iOS - 未收到 UIEventTypeRemoteControl 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083940/

相关文章:

ios - Swift 2.0 中的 UIView.animateWithDuration?

ios - 使用 TableView 或 CollectionView 时的 Swift、Slim Controller 代码

jquery 转动 "on"和 "off"事件处理程序

events - 取消订阅事件的事件处理程序

json - ZF2 查看策略

jquery - 我是否应该担心附加大量不需要的事件处理程序/监听器?

ios - 如何启用/禁用特定文本字段的 OneTimeCode 功能?

ios - 使用标准键盘作为自定义 iOS 8 键盘的模板

javascript - 警报和监听点击事件时出现问题

javascript - 在 javascript 中处理 Canvas 的 OnClick 事件并获取其坐标