使用 Swift 和 AVPlayer,如何通过代码添加和删除视频?

Posted

技术标签:

【中文标题】使用 Swift 和 AVPlayer,如何通过代码添加和删除视频?【英文标题】:Using Swift with AVPlayer, how do you add and remove a video via code? 【发布时间】:2015-09-22 20:29:06 【问题描述】:

我是 Swift 新手,正在尝试向视图添加视频,然后在发送“stopScreenSaver”通知时将其删除。除了我去删除视频层(playerLayer.removeFromSuperlayer())之外,一切似乎都很好。

任何指导将不胜感激。我觉得我在这里缺少一些用于添加和删除图层的基本概念!

    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()
            
    


【问题讨论】:

调度到主队列。 dispatch_async(dispatch_get_main_queue()) player.pause() playerLayer.removeFromSuperlayer() ) 【参考方案1】:

使用调度解决了我遇到的问题。

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】:

试试这个

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

【讨论】:

以上是关于使用 Swift 和 AVPlayer,如何通过代码添加和删除视频?的主要内容,如果未能解决你的问题,请参考以下文章

Swift/AppleTV4:如何将 AVPlayer 功能连接到 AVPlayerViewController?

如何在其他视图中使用 Swift 中的 AVPlayer 暂停和播放音乐

如何在 iOS swift 中使用 AVPlayer 请求发送参数?

使用swift 3点击单元格时如何在Avplayer中播放来自url的视频

如何在 swift 3 中将滑动手势添加到 AVPlayer

当您离开视图时如何在 Swift 中停用 AVPlayer