applicationDidBecomeActive时恢复AVPlayer视频?

时间:2017-05-12 18:21:17

标签: ios iphone swift avplayer

目前在使用AVPlayer的视图中播放视频作为我的背景,但当我返回主屏幕然后重新打开应用时,视频暂停,我无法恢复。当然关闭应用程序并重新打开修复它,但实际上我需要它在用户返回应用程序时恢复。我尝试在AppDelegate中使用applicationDidBecomeActive,创建一个链接到我的viewcontroller的变量,然后调用avPlayer.play()。然而,这会抛出一个错误,说找到了零。我还尝试在applicationDidBecomeActive中调用viewDidLoad(),这会给我一个BAD INSTRUCTION错误,即使代码在没有从委托中调用它时也能正常工作。
非常感谢任何帮助!

的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

    }

1 个答案:

答案 0 :(得分:1)

像这样设置一次通知。

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

然后处理你在这里播放的音乐

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