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

Swift 4 Unable to simultaneously satisfy constraints when using AVPlayerViewController()

我正在以编程方式为我的应用程序添加介绍视频。我收到以下警告。任何想法我做错了什么? 我对 swift 和 iOS 开发还很陌生。

2018-04-19 13:11:56.295952+0200 [1779:395252] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", " (active, names: '|':UIStackView:0x15dd29110 )>", "", " (active, names: '|':UIStackView:0x15dd29110 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2018-04-19 13:11:56.296790+0200 [1779:395252] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "UILayoutGuide:0x1d41bd180'UIViewLayoutMarginsGuide'
(active, names: '|':UIStackView:0x15dd29110 )>", " (active, names: '|':UIStackView:0x15dd29110 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

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

我最终通过使用 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)

}