iOS - UIEventTypeRemoteControl事件未收到 [英] iOS - UIEventTypeRemoteControl events not received

查看:212
本文介绍了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播放某种音频之后才能捕获遥控器,或者播放具有音频的视频文件。 here 中没有记录

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆