1,允許接受Remote事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
這句很重要,不然有些裝置不響應
- (BOOL) canBecomeFirstResponder {
return YES;
}
2,處理輸入事件:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
//do something
break;
case UIEventSubtypeRemoteControlPreviousTrack:
//do something
break;
case UIEventSubtypeRemoteControlNextTrack:
//do something
break;
default:
break;
}
}
} 3,在使用完畢的時候停止接受Remote事件
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
4,附上完整的事件類型代碼,供大家使用
typedef enum {
UIEventSubtypeNone = 0,
UIEventSubtypeMotionShake = 1,
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
} UIEventSubtype;
好了,關於耳機模式的使用就說到這裡了。