AVPlayer在后台播放视频 [英] AVPlayer play video in background

查看:79
本文介绍了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.

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

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