I have MPMoviePlayerController that should play video's audio in background and should be controlled by the multitasking play/pause controls.
After updating .plist file with Required background modes
and calling the following:
- (void)startBackgroundStreaming
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
NSError *activationError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&activationError];
[audioSession setActive:YES error:&activationError];
}
The app icon appears in the multitasking play/pause bar, but these buttons don't respond.
Thanks!
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;
}
}
}