私はAppDelegate(didFinishLaunching)にこれを持っています:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
関連するView Controllerでイベントを処理しようとしましたが、むらがありました(最初の応答者であっても、一部の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 のドキュメントに記載されているように、オーディオ コントロールを使用してリモート コントロール イベントをシミュレートできます (ホーム ボタンを 2 回タップし、下にスクロールします)。再生または一時停止を押すと、iTunes ライブラリからオーディオが再生されます。Appleヘッドフォンのボタンも試しました。何もない。
私がやりたいことは、リモコンの再生/一時停止ボタンを検出し、イベントを処理することだけです。これらのイベントをキャッチするには、他に何をする必要がありますか?