播放视频使用Avplayer背景(Play Video in Background using Av

2019-07-21 16:15发布

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

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

Answer 1:

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

此方法支持一切准备:

  • 屏幕由用户锁定;
  • Home键按下;
  • 切换到其它应用程序。

只要你有一个实例AVPlayer运行iOS防止设备的自动锁定。

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

然后把你的AppDelegate.m成- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

这些方法

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

#import <AVFoundation/AVFoundation.h>

然后在您的视图控制器控制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;
    }
}

需要另一个特技如果用户按压主页按钮(在这种情况下,再现被暂停与淡出)恢复再现。

当你控制视频的再现(我有play:pause:方法)集

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

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

和将被调用相应的方法,该方法将启动一个定时器并恢复再现。

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


Answer 2:

当应用程序在后台,你可以做这样的背景下任务:

1>音频
2>位置
3> VOIP
4>报摊内容
5>外部附件
6>蓝牙中央
7>蓝牙周
看到此链接,

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
http://www.rideitdown.com/2012/10/how-to-listen-youtube-videos-in.html

这也可以帮助你: AVPlayer在后台播放视频



Answer 3:

尝试在你的代码applicationDidEnterBackground

UIApplication *app = [UIApplication sharedApplication]; 
bgTask = 0;

backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(backgroundTask) userInfo:nil repeats:YES];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
[app endBackgroundTask:bgTask];

我发现它的地方在堆栈上对我的作品

另外,请查阅本教程涵盖背景模式,包括背景音乐.. http://www.raywenderlich.com/29948/backgrounding-for-ios



Answer 4:

上述答案暂停视频用于第二,当应用程序进入背景,但通过使用在其下方提及的方法保持视频播放应用程序的同时前进到背景而没有任何毛刺。

如果AVPlayer的当前项被显示设备的显示器上的视频,所述AVPlayer的播放时,应用程序被发送到后台自动暂停。 有两种方法来防止这种停顿:

  1. 禁用玩家项目的视频轨(仅基于文件的内容)。
  2. 将其从相关AVPlayer的AVPlayerLayer(设置AVPlayerLayer玩家属性无)。

参考

播放,而在后台使用AV基金会在iOS上媒体



Answer 5:

您可以使用下面列出的相应代码achive您的要求。

斯威夫特:4.2

只是,创建的子类UIViewController

import UIKit
import AVFoundation

class VideoPlayer:UIViewController{
    var avPlayer: AVPlayer!
    var avPlayerLayer: AVPlayerLayer!
    var paused: Bool = false

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.backgroundColor = .clear
        avPlayer?.play()
        paused = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let theURL = Bundle.main.url(forResource:"VIDEO_NAME", withExtension: "VIDEO_TYPE") //VIDEO_TYPE -> MP4

        avPlayer = AVPlayer(url: theURL!)
        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        avPlayer.volume = 0
        avPlayer.actionAtItemEnd = .none

        avPlayerLayer.frame = view.layer.bounds
        view.backgroundColor = .clear
        view.layer.insertSublayer(avPlayerLayer, at: 0)

        addPlayerNotifications()
    }

    deinit {
        removePlayerNotifations()
    }

    func addPlayerNotifications() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(playerItemDidReachEnd(notification:)),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                               object: avPlayer.currentItem)
        NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
    }

    func removePlayerNotifations() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
        NotificationCenter.default.removeObserver(self, name:UIApplication.willEnterForegroundNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name:UIApplication.didEnterBackgroundNotification, object: nil)
    }

    @objc func playerItemDidReachEnd(notification: Notification) {
        let p: AVPlayerItem = notification.object as! AVPlayerItem
        p.seek(to: CMTime.zero)
    }


    //App enter in forground.
    @objc func applicationWillEnterForeground(_ notification: Notification) {
        paused = false
        avPlayer?.play()
    }

    //App enter in forground.
    @objc func applicationDidEnterBackground(_ notification: Notification) {
        paused = true
        avPlayer?.pause()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        avPlayer.pause()
        paused = true
    }
}

现在,Almoast完成。 只要创建UIViewcontroller通过如下使用子类类。

class Classname: VideoPlayer{
   override func viewDidLoad() {
        super.viewDidLoad()
    }
}


文章来源: Play Video in Background using Avplayer