AVPlayer在后台播放视频

问题描述:

我正在使用AVPlayer在我的应用中播放视频,现在我想让该视频在后台模式下播放.

I am using AVPlayer to play a video in my app,now i want to let the video play in background mode.

这就是我放入的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

[[AVAudioSession sharedInstance] setDelegate: self];    
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

,我也添加到plist中: 所需的背景模式->应用播放音频

and i also add to the plist: Required background modes -> App plays audio

我也添加了它:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            NSLog(@"4");
            break;
        case UIEventSubtypeRemoteControlPlay:
            break;
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"3");
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"2");
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            NSLog(@"1");
            break;
        default:
            break;
    }
}

当我将应用程序移至后台并按下按钮时,nslog将打印到控制台

And when i move the app to the background and press the buttons the nslog print to the console

我需要添加其他内容吗?

Did i need to add something else?

只需添加[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];和其他一些调整即可.都是此处.

Just add [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; and some other tweaks. It's all here.