iOS MPMoviePlayerController playing audio in background

前端 未结 1 1757
清歌不尽
清歌不尽 2020-12-09 07:03

I have MPMoviePlayerController that should play video\'s audio in background and should be controlled by the multitasking play/pause controls.

After updating .plist

相关标签:
1条回答
  • 2020-12-09 07:34

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题