播放视频使用Avplayer背景 [英] Play Video in Background using Avplayer

查看:306
本文介绍了播放视频使用Avplayer背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序,我想保持播放视频时,应用程序在后台模式下输入。

In my iPhone application I want to keep play video when application enter in background mode.

我使用AVPlayer并没有找到任何方式在后台播放视频。我会很感激,如果任何人可以帮我在这。
谢谢

I am using AVPlayer and not finding any way to play video in background. I will be very grateful if any one can help me in this. Thanks

推荐答案

通过惊讶,我可以说,这是可以实现的,我只是做了它。

With surprise I can say that this can be achieved and I just did it.

这个方法支持一切准备:

This method supports all the possibilities:


  • 将用户锁定的屏幕;

  • 主页按钮pressed;

  • 切换到其它应用程序。

只要你有 AVPlayer 运行iOS prevents自动装置的锁的一个实例。

As long as you have an instance of AVPlayer running iOS prevents auto lock of the device.

首先,你需要配置应用程序以支持从Info.plist文件的 UIBackgroundModes 数组中添加音频背景音频元素。

First you need to configure the application to support audio background from the Info.plist file adding in the UIBackgroundModes array the audio element.

然后把你的AppDelegate.m到 - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions:

Then put in your AppDelegate.m into - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

这些方法

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

#进口< AVFoundation / AVFoundation.h>

然后在您的视图控制器控制 AVPlayer

Then in your view controller that controls AVPlayer

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

- (void)viewWillDisappear:(BOOL)animated
{
    [mPlayer pause];    
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

然后作出回应

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            if([mPlayer rate] == 0){
                [mPlayer play];
            } else {
                [mPlayer pause];
            }
            break;
        case UIEventSubtypeRemoteControlPlay:
            [mPlayer play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [mPlayer pause];
            break;
        default:
            break;
    }
}

需要另一个伎俩恢复再现​​,如果用户presses home键(在这种情况下,再现被暂停使用淡出)。

Another trick is needed to resume the reproduction if the user presses the home button (in which case the reproduction is suspended with a fade out).

在控制视频的播放(我有游戏暂停:方法)设置

When you control the reproduction of the video (I have play: and pause: methods) set

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

和对应的方法要被调用,将启动一个计时器,并恢复再现。

and the corresponding method to be invoked that will launch a timer and resume the reproduction.

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
    [mPlayer performSelector:@selector(play) withObject:nil afterDelay:0.01];
}

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

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