remote-control
可以讓使用者通過外部介面控制應用程式音樂, 官方建議的做法是在View顯示的時候接收remote-control事件
- (void)viewDidAppear:(BOOL)animated<br />{<br /> [super viewDidAppear:animated];<br /> [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];<br /> [self becomeFirstResponder];<br />}
然後在View消失的時候取消接收remote-control 事件
- (void)viewWillDisappear:(BOOL)animated<br />{<br /> [super viewWillDisappear:animated];<br /> [[UIApplication sharedApplication] endReceivingRemoteControlEvents];<br /> [self resignFirstResponder];<br />}
還有一點,接收reomte-control的resp必須是first responder,所以要複寫canBecameFirstResponder
- (BOOL)canBecomeFirstResponder<br />{<br /> return YES;<br />}
另一簡潔的方法是 , 派生UIWindow並複寫它的- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent方法
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {</p><p> if (receivedEvent.type == UIEventTypeRemoteControl) {</p><p> switch (receivedEvent.subtype) {</p><p> case UIEventSubtypeRemoteControlTogglePlayPause:<br /> [self playOrStop: nil];<br /> break;</p><p> case UIEventSubtypeRemoteControlPreviousTrack:<br /> [self previousTrack: nil];<br /> break;</p><p> case UIEventSubtypeRemoteControlNextTrack:<br /> [self nextTrack: nil];<br /> break;</p><p> default:<br /> break;<br /> }<br /> }<br />}
是不是更簡單?!