我如何知道用户点击iPhone中播放控件的快进和快退button

我想实施以下的事情,

  1. 应用程序在后台运行音乐或video(使用MPMoviePlayerController )。
  2. 用户双击主页button,进入显示播放控制的第一个屏幕(快退,播放或暂停,快进button)
  3. 用户点击快退或快进button。 然后,应用播放上一个或下一个音乐或video。

第三步,我应该知道哪个button被点击。 (正如我自然知道,当前正在播放的项目已暂停,停止..使用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; 

这可能是一个非常晚的答案,但正如我注意到的,关于audio播放和遥控器的Q / A并不多,所以我希望我的答案能够帮助其他有相同问题的人:

目前我正在使用AVAudioPlayer ,但是- (void)remoteControlReceivedWithEvent:(UIEvent *)event的远程控制方法不能与您使用的播放器的types有关。

要获得locking屏幕上的前进和后退button,请执行以下操作:

在你的视图控制器的viewDidLoad方法中添加下面的代码:

 //Make sure the system follows our playback status - to support the playback when the app enters the background mode. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

然后添加这些方法: viewDidAppear: :(如果尚未实现)

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //Once the view has loaded then we can register to begin recieving controls and we can become the first responder [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; } 

viewWillDisappear:如果尚未实现)

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //End recieving events [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; } 

和:

 //Make sure we can recieve remote control events - (BOOL)canBecomeFirstResponder { return YES; } - (void)remoteControlReceivedWithEvent:(UIEvent *)event { //if it is a remote control event handle it correctly if (event.type == UIEventTypeRemoteControl) { if (event.subtype == UIEventSubtypeRemoteControlPlay) { [self playAudio]; } else if (event.subtype == UIEventSubtypeRemoteControlPause) { [self pauseAudio]; } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) { [self togglePlayPause]; } else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward) { [self rewindTheAudio]; //You must implement 15" rewinding in this method. } else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward) { [self fastForwardTheAudio]; //You must implement 15" fastforwarding in this method. } } } 

这在我的应用程序中工作正常,但是如果您希望能够在所有视图控制器中接收远程控制事件,则应将其设置在AppDelegate

注意! 此时此代码正常工作,但我看到两个名为UIEventSubtypeRemoteControlEndSeekingBackwardUIEventSubtypeRemoteControlEndSeekingBackward子types。 我不确定是否需要执行,如果有人知道,请告诉我们。