[iPhone Dev] iPhone의 iPod Playback control의 prev, next (fast rewid, fast forward) 버튼 클릭 이벤트 받기

No Comments

iOS4에서 백그라운드로 음악을 플레이하거나 스트리밍을 할 때, 아래 그림과 같이 Playback control의 Prev, Play, Next 버튼이 클릭될 때, 이벤트를 받는 방법이다.

-_-;; Stack Overflow에 질문을 했다가 혼 자 답변했다. -_-;
답은 UIApplication의 beginReceivingRemoteControlEvents를 이용하는 것이다.
아래를 viewDidAppear: 정도에 넣어 준다.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

해당 View Controller에 아래를 구현하고 YES를 return한다. (Defaul는 NO이다.)

- (BOOL)canBecomeFirstResponder {
 return YES;
}

그리고 나면 Control에 있는 버튼이 클릭 될 때 마다 아래 method가 호출된다.

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {

 if( event.type == UIEventTypeRemoteControl ) {
 NSLog(@"sub type: %d", event.subtype);
 }
}

event.subtype은 아래와 같으니 원하는 것이 다 있을 것이다.

typedef enum {
 // available in iPhone OS 3.0
 UIEventSubtypeNone                              = 0,

 // for UIEventTypeMotion, available in iPhone OS 3.0
 UIEventSubtypeMotionShake                       = 1,

 // for UIEventTypeRemoteControl, available in iPhone OS 4.0
 UIEventSubtypeRemoteControlPlay                 = 100,
 UIEventSubtypeRemoteControlPause                = 101,
 UIEventSubtypeRemoteControlStop                 = 102,
 UIEventSubtypeRemoteControlTogglePlayPause      = 103,
 UIEventSubtypeRemoteControlNextTrack            = 104,
 UIEventSubtypeRemoteControlPreviousTrack        = 105,
 UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
 UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
 UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
 UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;

Ref

1. How can I know users click fast forward and fast rewind buttons on the playback controls in iPhone

2. How do I catch the MPMoviePlayer next button click event while in fullscreen mode on the iPad?