当applicationDidBecomeActive恢复AVPlayer视频?

问题描述:

当前在使用AVPlayer的视图中播放视频作为我的背景,但是当我回到主屏幕然后重新打开应用程序时,视频暂停,我无法恢复。当然,关闭应用程序并重新打开它可以修复它,但当用户返回到应用程序时,我确实需要恢复它。
我已经尝试在AppDelegate中使用applicationDidBecomeActive,通过创建一个链接到我的viewcontroller的变量,然后调用avPlayer.play()。然而,这然后抛出一个错误说发现零。我也尝试在applicationDidBecomeActive中调用viewDidLoad(),然后给我一个不好的指令错误,即使代码没有从委托调用它时完美工作。
任何帮助将不胜感激!当applicationDidBecomeActive恢复AVPlayer视频?

的ViewController:

import UIKit 
import AVFoundation 


class ViewController: UIViewController, CLLocationManagerDelegate { 

    var avPlayer: AVPlayer! 
    var avPlayerLayer: AVPlayerLayer! 
    var paused: Bool = false 
    var foregroundNotification: NSObjectProtocol! 

    //viewDidLoad 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     NotificationCenter.default.addObserver(self, selector:#selector(ViewController.viewDidLoad), name:NSNotification.Name.UIApplicationWillEnterForeground, object:UIApplication.shared) 


     //Rotate icon 

     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = 360.0 
     rotateAnimation.repeatCount = HUGE 
     rotateAnimation.duration = 450 


     checkInButton.layer.add(rotateAnimation, forKey: "myAnimationKey"); 

     //Play background video 

     let theURL = Bundle.main.url(forResource:"city2", withExtension: "mp4") 

     avPlayer = AVPlayer(url: theURL!) 
     avPlayerLayer = AVPlayerLayer(player: avPlayer) 
     avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
     avPlayer.volume = 0 
     avPlayer.actionAtItemEnd = .none 
     //avPlayer.rate = 1 

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

     NotificationCenter.default.addObserver(self, 
               selector: #selector(playerItemDidReachEnd(notification:)), 
               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, 
               object: avPlayer.currentItem) 

} 


    func playerItemDidReachEnd(notification: Notification) { 
     let p: AVPlayerItem = notification.object as! AVPlayerItem 
     p.seek(to: kCMTimeZero) 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     avPlayer.play() 
     paused = false 

    } 

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

} 
} 

代表:

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    var vc = ViewController() 


    func applicationDidBecomeActive(_ application: UIApplication) { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 

     vc.avPlayer.play() 
     vc.paused = false 

    } 
+0

尝试增加与NameWillEnterForeground的通知中心在ViewDidAppear或viewWillAppear中的功能。并创建一个函数来启动ViewController中的avPlayer.play()。 –

设置通知曾这样。

NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) 

然后处理自己的音乐播放这里

func appMovedToForeground() { 
    //do you play stuff here 
}