Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

iphone - iOS MPMoviePlayerController playing audio in background

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...