iOS MPMoviePlayerController在后台播放音频 [英] iOS MPMoviePlayerController playing audio in background

查看:145
本文介绍了iOS MPMoviePlayerController在后台播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

用<$ c $更新.plist文件后c>所需的背景模式并调用以下内容:

- (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.

谢谢!

推荐答案

这个难题的缺失部分是处理你收到的遥控事件。您可以通过在应用程序委托中实现 - (void)remoteControlReceivedWithEvent:(UIEvent *)事件方法来完成此操作。最简单的形式如下:

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

这篇关于iOS MPMoviePlayerController在后台播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆