如何知道用户在iPhone中的播放控件上单击快进和快退按钮

alo*_*nes 7 objective-c playback mpmovieplayercontroller avfoundation ios

我想实现以下内容,

  1. 应用程序正在MPMoviePlayerController后台运行音乐或视频(使用).
  2. 用户双击主页按钮并转到显示播放控件的第一个屏幕(快退,播放或暂停,快进按钮)
  3. 用户单击快退或快进按钮.然后app播放上一个或下一个音乐或视频.

对于第3步,我应该知道单击了哪个按钮.(我自然知道,当前正在播放的项目暂停,停止..使用MPMoviePlayerPlaybackStateDidChangeNotification通知).

我应该注册哪个通知?还是有其他方法吗?

alo*_*nes 6

我自己得到了答案.

那是使用UIApplication的beginReceivingRemoteControlEvents.

在适当的地方(如viewWillAppear :)放入以下代码

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
Run Code Online (Sandbox Code Playgroud)

并且视图控制器应该实现以下方法返回YES

- (BOOL)canBecomeFirstResponder {
    return YES; 
}
Run Code Online (Sandbox Code Playgroud)

然后您可以通过以下方法接收远程控制器事件.

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

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

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;
Run Code Online (Sandbox Code Playgroud)