0

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

- (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;
    }
}
 }
4

1 回答 1

-1

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

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

于 2013-03-15T05:01:07.383 回答