Using Swift with AVPlayer, how do you add and remove a video via code?

不羁岁月 提交于 2019-12-12 12:50:23

问题


I'm new to Swift and am trying to add a video to the view and then remove it when my "stopScreenSaver" notification is dispatched. All seems to work well except for when I go to remove the video layer (playerLayer.removeFromSuperlayer()).

Any guidance would be appreciated. I feel like I'm missing some basic concept here for adding and removing the layer!

    import UIKit
    import AVFoundation
    import QuartzCore
    import CoreMedia

    class ViewController: UIViewController {

        let contentURL = NSBundle.mainBundle().URLForResource("testvideo", withExtension: "mp4")
        var player = AVPlayer()
        var playerLayer = AVPlayerLayer()
        let screenSize : CGRect = UIScreen.mainScreen().bounds

        override func viewDidLoad() {
            super.viewDidLoad()
            // Used for starting and stopping the videos related to the screen saver
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "playScreenSaver:", name: "playScreenSaverID", object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopScreenSaver:", name: "stopScreenSaverID", object: nil)
}

        override func viewDidAppear(animated: Bool) {
            // Player
            player = AVPlayer(URL: contentURL!)

            // Layer for display… Video plays at the full size of the iPad
            playerLayer = AVPlayerLayer(player: player)
            var view = UIView(frame: CGRectMake(0, 0, screenSize.width, screenSize.height))
            self.view.layer.addSublayer(playerLayer)
            playerLayer.frame = view.bounds
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }


    func playScreenSaver(notification: NSNotification){
            print("play")
            dispatch_async(dispatch_get_main_queue()) {
               self.view.layer.addSublayer(self.playerLayer!)
               self.player!.play()
            }
    }

    func stopScreenSaver(notification: NSNotification){
            print("pause")
            dispatch_async(dispatch_get_main_queue()) {
               self.player!.pause()
               self.playerLayer!.removeFromSuperlayer()
            }
    }

}

回答1:


Using the dispatch fixed the issue I was having.

dispatch_async(dispatch_get_main_queue()) {
    self.player!.pause()
    self.playerLayer!.removeFromSuperlayer()
 }



回答2:


override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        DispatchQueue.main.async {
            self.player.currentItem?.removeObserver(self, forKeyPath: "duration", context: nil)
            self.player!.pause()
            self.playerLayer!.removeFromSuperlayer()
        }
    }



回答3:


Try this

 override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    DispatchQueue.main.async {
        self.player?.pause()
        self.playerLayer?.removeFromSuperlayer()
    }
}


来源:https://stackoverflow.com/questions/32726269/using-swift-with-avplayer-how-do-you-add-and-remove-a-video-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!