Swift 4使用AVPlayerViewController()时无法同时满足约束

时间:2018-04-19 11:30:24

标签: ios swift swift4 avplayerviewcontroller

我以编程方式为我的应用添加了一个介绍视频。我得到以下警告。我有什么想法,我做错了什么? 我对swift和iOS开发非常陌生。

  

2018-04-19 13:11:56.295952 + 0200 [1779:395252] [LayoutConstraints]   无法同时满足约束。可能至少有一个   以下列表中的约束是您不想要的。尝试   这个:(1)看看每个约束并试着弄清楚你是哪个   不要指望; (2)找到添加了不需要的约束的代码   或约束并修复它。 (       "&#34 ;,       "&#34 ;,       "   (有效,姓名:' |':UIStackView:0x15dd29110)>",       "&#34 ;,       "   (有效,姓名:' |':UIStackView:0x15dd29110)>" )

     

将尝试通过违反约束来恢复   

     

在UIViewAlertForUnsatisfiableConstraints处创建一个符号断点   在调试器中捕获它。方法中的   在UIView中列出的UIConstraintBasedLayoutDebugging类别    也可能有所帮助。 2018-04-19 13:11:56.296790 + 0200   [1779:395252] [LayoutConstraints]无法同时满足   限制。可能至少有一个限制因素   以下列表是您不想要的。试试这个:(1)看看每一个   约束并试图找出你不期望的东西; (2)找到   添加了不需要的约束或约束并修复它的代码。   (       "&#34 ;,       "&#34 ;,       "&#34 ;,       " UILayoutGuide:0x1d41bd180' UIViewLayoutMarginsGuide'
  (有效,姓名:' |':UIStackView:0x15dd29110)>",       "   (有效,姓名:' |':UIStackView:0x15dd29110)>" )

     

将尝试通过违反约束来恢复   

     

在UIViewAlertForUnsatisfiableConstraints处创建一个符号断点   在调试器中捕获它。方法中的   在UIView中列出的UIConstraintBasedLayoutDebugging类别    也可能会有帮助。

var movieController: AVPlayer?
let playerViewController = AVPlayerViewController()

override func viewDidLoad() {
    playVideo()
}

func playVideo() {
    let path = Bundle.main.path(forResource: appData.videoFile, ofType: appData.videoExtension)
    let url = URL(fileURLWithPath: path!)
    movieController = AVPlayer(url: url)
    NotificationCenter.default.addObserver(self, selector:#selector(movieFinished),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: movieController?.currentItem)
    playerViewController.player = self.movieController
    playerViewController.delegate = self
    playerViewController.view.frame = self.view.bounds
    playerViewController.showsPlaybackControls = false
    playerViewController.videoGravity = "AVLayerVideoGravityResizeAspectFill"
    playerViewController.view.translatesAutoresizingMaskIntoConstraints = false
    playerViewController.player?.play()
    self.view.addSubview(playerViewController.view)
}

@objc func movieFinished() {
    let value = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    UIViewController.attemptRotationToDeviceOrientation()
    UIView.animate(withDuration: 1.5, delay: 0, options: .curveEaseIn, animations: {
        self.playerViewController.view.alpha = 0.0
    }, completion: { (finished: Bool) in
        self.playerViewController.view.removeFromSuperview()
    })
}

1 个答案:

答案 0 :(得分:0)

我最终通过使用AVPlayerLayer和AVPlayerItem来解决这个问题。

var player:AVPlayer?
var playerItem:AVPlayerItem?
let playerLayer:AVPlayerLayer? = nil

override func loadView() {
    super.loadView()
    startWebiew()
}

func playVideo() {
    let path = Bundle.main.path(forResource: appData.videoFile, ofType: appData.videoExtension)
    let url = URL(fileURLWithPath: path!)
    let playerItem:AVPlayerItem = AVPlayerItem(url: url)
    player = AVPlayer(playerItem: playerItem)
    let playerLayer=AVPlayerLayer(player: player!)
    playerLayer.frame = view.bounds
    playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    playerLayer.player?.play()
    view.layer.addSublayer(playerLayer)

}