gpt4 book ai didi

iOS - 未收到 UIEventTypeRemoteControl 事件

转载 作者:可可西里 更新时间:2023-11-01 05:10:05 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com