Iphone iOS MPMoviePlayerController在后台播放音频

Iphone iOS MPMoviePlayerController在后台播放音频,iphone,ios,mpmovieplayercontroller,multitasking,background-audio,Iphone,Ios,Mpmovieplayercontroller,Multitasking,Background Audio,我有一个MPMoviePlayerController,它应该在后台播放视频音频,并且应该由多任务播放/暂停控件控制 使用所需的后台模式更新.plist文件后并调用以下命令: - (void)startBackgroundStreaming { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; NSError *activ

我有一个MPMoviePlayerController,它应该在后台播放视频音频,并且应该由多任务播放/暂停控件控制

使用
所需的后台模式更新.plist文件后
并调用以下命令:

- (void)startBackgroundStreaming
{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

    NSError *activationError = nil;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&activationError];
    [audioSession setActive:YES error:&activationError];
}

应用程序图标出现在多任务播放/暂停栏中,但这些按钮没有响应


谢谢

拼图中缺少的部分是处理您正在接收的远程控制事件。您可以通过在应用程序委托中实现
-(void)remoteControlReceivedWithEvent:(UIEvent*)event
方法来实现这一点。最简单的形式是:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
    if (event.type == UIEventTypeRemoteControl){
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                // Toggle play pause
                break;
            default:
                break;
        }
    }
}
但是,在应用程序委托上调用此方法,但您始终可以发布以事件为对象的通知,以便拥有电影播放器控制器的视图控制器可以获取事件,如下所示:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoteControlEventReceived" object:event];
}
然后在指定给通知的侦听器方法中获取事件对象

-(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;
        }
    }
}
可以从视图控制器调用-(void)remoteControlReceivedWithEvent:(UIEvent*)事件,而不是从AppDelegate调用