检测耳机按钮按下

时间:2012-10-06 17:31:51

标签: iphone objective-c xcode ipad

我需要在我的应用上使用耳机按钮按下除多媒体之外的其他内容。这是我到目前为止所做的,而且它不起作用。当我按下耳机按钮时,我的应用程序没有响应,而是播放音乐......

- (BOOL)canBecomeFirstResponder {

return YES;
}

- (void)viewDidAppear:(BOOL)animated {

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
if ([self canBecomeFirstResponder]) {
    [self becomeFirstResponder];
    NSLog(@"became first responder");
}

}

 - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent
 {
if (theEvent.type == UIEventTypeRemoteControl) {
    switch(theEvent.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            NSLog(@"Hello");

        case UIEventSubtypeRemoteControlPlay:
            NSLog(@"Hello 2");
            break;
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"Hello 3");
            break;
        case UIEventSubtypeRemoteControlStop:
            NSLog(@"Hello 4");
            break;
        default:
            return;
    }
}
 }

1 个答案:

答案 0 :(得分:-1)

@ user1724494您使用的代码仅适用于在后台运行的应用,因为该事件是我们只能在后台操作中处理的远程事件。

请参阅Apple文档http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html