iOS - 未收到 UIEventTypeRemoteControl 事件

时间:2023-03-20
本文介绍了iOS - 未收到 UIEventTypeRemoteControl 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 AppDelegate (didFinishLaunching) 中有这个:

I have this in the AppDelegate (didFinishLaunching):

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

我尝试在相关视图控制器中处理事件,但结果参差不齐(一些视图控制器会收到事件,而另一些则不会,即使他们是第一响应者).我尝试子类化 UIApplication.那没有用.现在我正在尝试继承 UIWindow 并执行此操作(请参阅评论):

I tried handling the events in the relevant view controllers but that was spotty (some view controllers would get the events and others wouldn't, even when they were first responders). I tried subclassing UIApplication. That didn't work. Now I'm trying to subclass UIWindow and do this (see the comments):

- (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;
    }
}
}

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

I have tried both with and without this in the info plist:

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

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

Doesn't make a difference. As the Apple docs state, you can simulate remote control events with the audio controls (double tap home button and scroll to them on the bottom). When I press play or pause, it just plays audio from my iTunes library. I have tried the buttons on the Apple headphones as well. Nothing.

我想要做的就是检测遥控器上的播放/暂停按钮,并处理该事件.我还需要做什么才能捕捉到这些事件?

All I want to do is detect the play/pause button on a remote control, and handle the event. What else do I need to do to catch these events?

推荐答案

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

ok, a few more hours of my life into black hole of Apple. Turns out that you can only capture remote control events after you have played audio of some kind using the AVAudioPlayer, or if you play a video file that has audio. This is not documented anywhere here.

因此,在我尝试捕获远程控制事件之前,我必须先播放一个虚拟静音音频文件.希望这可以帮助某人.

So in my case I have to play a dummy silent audio file first before I try to capture remote control events. Hope this helps someone down the line.

(更新)请注意,更新的文档现在声明如下:

(Update) Please note that the updated documentation now states the following:

要接收远程控制事件,您的应用必须做三件事:

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 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:MapView 中的 OnTouch 仅在第一次触发 下一篇:Android:为 ListView 中的每个项目添加事件侦听器

相关文章