【问题标题】:iOS MPMoviePlayerController playing audio in backgroundiOS MPMoviePlayerController 在后台播放音频
【发布时间】:2013-03-10 13:28:30
【问题描述】:

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

使用Required background modes 更新 .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];

}

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

谢谢!

【问题讨论】:

    标签: iphone ios mpmovieplayercontroller multitasking background-audio


    【解决方案1】:

    难题中缺少的部分是处理您收到的远程控制事件。您可以通过在应用程序委托中实现 -(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 *)event 可以从 View Controller 调用,而不是 AppDelegate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多