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

alones picture alones · Aug 29, 2010 · Viewed 10.8k times · Source

I want to implement the following things,

  1. App is running a music or video (using MPMoviePlayerController) in background.
  2. User double clicks the home button and go to the first screen showing playback controls (fast rewind, play or pause, fast forward buttons)
  3. User click fast rewind or fast forward button. Then app play previous or next music or video.

For the 3rd step, I should know which button is clicked. (As I naturally know, the currently playing item is paused, stopped.. using MPMoviePlayerPlaybackStateDidChangeNotification notification).

Which notification should I register? Or are there any other approaches?

Answer

alones picture alones · Aug 30, 2010

I got the answer by myself.

That is using UIApplication's beginReceivingRemoteControlEvents.

In an appropriate place (like viewWillAppear:) put the following code

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

And the view controller should implement the following method returning YES

- (BOOL)canBecomeFirstResponder {
    return YES; 
}

And then you can receive remote controller event in the following method.

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

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

And event.subtype is as below,

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;