我想实现以下内容,
> App正在后台运行音乐或视频(使用MPMoviePlayerController).
>用户双击主页按钮并转到显示播放控件的第一个屏幕(快退,播放或暂停,快进按钮)
>用户单击快退或快进按钮.
然后应用播放上一个或下一个音乐或视频.
对于第3步,我应该知道单击了哪个按钮.
(我自然知道,当前播放的项目已暂停,停止使用MPMoviePlayerPlaybackStateDidChangeNotification通知).
解决方法
我自己得到了答案.
那是使用UIApplication的beginReceivingRemoteControlEvents.
在适当的地方(如viewWillAppear :)放入以下代码
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder];
并且视图控制器应该实现以下方法返回YES
- (BOOL)canBecomeFirstResponder { return YES; }
然后您可以通过以下方法接收远程控制器事件.
- (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;