The missing piece of the puzzle is handling the remote control events you are receiving. You do this by implementing the -(void)remoteControlReceivedWithEvent:(UIEvent *)event
method in your application delegate. In its simplest form it would look like:
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
if (event.type == UIEventTypeRemoteControl){
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
// Toggle play pause
break;
default:
break;
}
}
}
However this method is called on the application delegate, but you can always post a notification with the event as the object so that the view controller that owns the movie player controller can get the event, like so:
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter] postNotificationName:@"RemoteControlEventReceived" object:event];
}
Then grab the event object in the listener method you assign to the notification.
-(void)remoteControlEventNotification:(NSNotification *)note{
UIEvent *event = note.object;
if (event.type == UIEventTypeRemoteControl){
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (_moviePlayerController.playbackState == MPMoviePlaybackStatePlaying){
[_moviePlayerController pause];
} else {
[_moviePlayerController play];
}
break;
// You get the idea.
default:
break;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…