我想检测iPhone上的耳机按钮,请参阅Detect headset button click on iPhone SDK。 我跟随http://developer.apple.com/library/IOS/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html, 然后我写了如下代码,但它无法正常工作!!
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
if (receivedEvent.type == UIEventTypeRemoteControl) {
NSLog(@"Remote Control Event");
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
break;
default:
break;
}
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
我的问题是我无法捕捉遥控事件。当单击耳机上的播放或暂停按钮时,控制台不会打印任何内容!谁能帮我?谢谢。
答案 0 :(得分:1)
您的Info.plist是否已更新,表明您支持音频?
添加新行,然后选择“必需的背景模式”。在其下/旁边显示的“项目0”中,选择“应用播放音频”。