問題描述
我在 AppDelegate (didFinishLaunching) 中有這個(gè):
I have this in the AppDelegate (didFinishLaunching):
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
我嘗試在相關(guān)視圖控制器中處理事件,但結(jié)果參差不齊(一些視圖控制器會(huì)收到事件,而另一些則不會(huì),即使他們是第一響應(yīng)者).我嘗試子類化 UIApplication.那沒有用.現(xiàn)在我正在嘗試?yán)^承 UIWindow 并執(zhí)行此操作(請參閱評(píng)論):
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;
}
}
}
我已經(jīng)在 info plist 中嘗試過使用和不使用它:
I have tried both with and without this in the info plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
沒有區(qū)別.正如 Apple 文檔所述,您可以使用音頻控件模擬遠(yuǎn)程控制事件(雙擊主頁按鈕并在底部滾動(dòng)到它們).當(dāng)我按下播放或暫停時(shí),它只會(huì)播放我的 iTunes 資料庫中的音頻.我也試過蘋果耳機(jī)上的按鈕.什么都沒有.
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?
推薦答案
好吧,我的生命中還有幾個(gè)小時(shí)進(jìn)入蘋果的黑洞.事實(shí)證明,您只能在使用 AVAudioPlayer 播放某種音頻或播放有音頻的視頻文件后才能捕獲遠(yuǎn)程控制事件.這在這里的任何地方都沒有記錄.
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.
因此,在我嘗試捕獲遠(yuǎn)程控制事件之前,我必須先播放一個(gè)虛擬靜音音頻文件.希望這可以幫助某人.
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.
(更新)請注意,更新的文檔現(xiàn)在聲明如下:
(Update) Please note that the updated documentation now states the following:
要接收遠(yuǎn)程控制事件,您的應(yīng)用必須做三件事:
To receive remote control events, your app must do three things:
- 成為第一響應(yīng)者.呈現(xiàn)多媒體內(nèi)容的視圖或視圖控制器必須是第一響應(yīng)者.
- 開啟遠(yuǎn)程控制事件的傳遞.您的應(yīng)用必須明確請求開始接收遠(yuǎn)程控制事件.
- 開始播放音頻.您的應(yīng)用必須是正在播放"應(yīng)用.重申,即使您的應(yīng)用是第一響應(yīng)者并且您已轉(zhuǎn)身在事件傳遞時(shí),您的應(yīng)用不會(huì)接收遠(yuǎn)程控制事件直到它開始播放音頻.
- 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.
這篇關(guān)于iOS - 未收到 UIEventTypeRemoteControl 事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!