AVPlayerController:全屏自动布局问题

Posted

技术标签:

【中文标题】AVPlayerController:全屏自动布局问题【英文标题】:AVPlayerController: Full screen Autolayout issues 【发布时间】:2016-05-07 10:23:52 【问题描述】:

我正在使用以下代码在我的 Swift 应用 (ios 9) 中播放视频:

        let fileUrl = NSBundle.mainBundle().URLForResource("welcome", withExtension: ".mp4")
        videoItem =  AVPlayerItem(URL: fileUrl!)
        videoPlayer = AVPlayer(playerItem: videoItem)
        videoPlayer.addObserver(self, forKeyPath: "rate", options: [.New], context: nil)

        videoPlayerController.player = videoPlayer
        videoPlayerController.showsPlaybackControls = false

        videoPlayerController.view.frame = CGRectMake(0, 0, playerView.frame.size.width, playerView.frame.size.height)
        playerView.insertSubview(videoPlayerController.view, belowSubview: videoCoverView)

        videoPlayerController.willMoveToParentViewController(self)
        addChildViewController(videoPlayerController)
        videoPlayerController.didMoveToParentViewController(self)

        playerView.bringSubviewToFront(btnPlay)

        let asset = AVAsset(URL: fileUrl!)
        let imageGenerator = AVAssetImageGenerator(asset: asset)
        let time = CMTime(seconds: asset.duration.seconds/2.0, preferredTimescale: 1)

        do 
            let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
            let coverImage = UIImage(CGImage: imageRef)
            videoCoverView.image = coverImage
         catch 

        

videoPlayerControllerAVPlayerViewController 的一个实例。 playerView 通过情节提要添加并在其上设置了自动布局。一旦 AVPlayerViewController 的播放器控件出现,以及当我尝试通过播放器控件进入全屏模式时,我就会遇到 Autloayout 问题。这是打印的日志。

2016-05-07 15:18:19.325 VoxTrain[2085:66307] 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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7fb6a2a3f2c0 h=-&- v=-&- _UIBackdropContentView:0x7fb6a2a02310.width == _UIBackdropView:0x7fb6a053bf90.width>",
    "<NSLayoutConstraint:0x7fb6a2992800 H:|-(14)-[UILabel:0x7fb6a2991220'Hi-Speed Scrubbing']   (Names: '|':_UIBackdropContentView:0x7fb6a2a02310 )>",
    "<NSLayoutConstraint:0x7fb6a2992850 H:[UILabel:0x7fb6a2991220'Hi-Speed Scrubbing']-(14)-|   (Names: '|':_UIBackdropContentView:0x7fb6a2a02310 )>",
    "<NSLayoutConstraint:0x7fb6a29649a0 H:|-(0)-[_UIBackdropView:0x7fb6a053bf90]   (Names: '|':UIView:0x7fb6a0549220 )>",
    "<NSLayoutConstraint:0x7fb6a298b8a0 H:[_UIBackdropView:0x7fb6a053bf90]-(0)-|   (Names: '|':UIView:0x7fb6a0549220 )>",
    "<NSLayoutConstraint:0x7fb6a296f440 H:|-(0)-[UIView:0x7fb6a0549220]   (Names: '|':AVAlphaUpdatingView:0x7fb6a053dae0 )>",
    "<NSLayoutConstraint:0x7fb6a296f490 H:[UIView:0x7fb6a0549220]-(0)-|   (Names: '|':AVAlphaUpdatingView:0x7fb6a053dae0 )>",
    "<NSLayoutConstraint:0x7fb6a2a40cc0 'UIView-Encapsulated-Layout-Width' H:[AVAlphaUpdatingView:0x7fb6a053dae0(0)]>"
)

在最初的方法之后,我尝试在videoPlayerController's 视图上设置autolayout,并将translatesAutoresizingMaskIntoConstraints 设置为false。但问题仍然存在。我该如何解决这些问题?

【问题讨论】:

复制:***.com/q/32860362/2415822 【参考方案1】:

使用此代码,

viewDidLoad 方法:

        let type : String! = "mp4"
        let targetURL : String? = NSBundle.mainBundle().pathForResource("Welcome", ofType: type)

        let videoURL = NSURL(fileURLWithPath:targetURL!)


        let player = AVPlayer(URL: videoURL)
        let playerController = AVPlayerViewController()

        playerController.player = player
        self.addChildViewController(playerController)
        self.playView.addSubview(playerController.view)
        playerController.view.frame = playView.bounds

        player.play()

下面是我的故事板视图,

我已经创建了 UIView,然后设置了解决的顶部、左侧、右侧和高度。它对我有用

希望对你有帮助

【讨论】:

不。我已经在按照不同的顺序做所有这些事情了。唯一的区别是我的 PlayerView 的高度与 ViewController 的视图成正比。视频运行流畅。只有当您尝试通过将其 showPlaybackControls 属性设置为 true 来显示 AVPlayerViewCONtroller 的本机控件时才会出现问题。现在,如果您尝试使用这些控件进入全屏模式,那么 Autolayout 日志将打印在控制台上。【参考方案2】:

问题是当您的 AVPlayerController 在屏幕上时,您尝试设置 showsPlaybackControls = true。这样做会创建系统 UI 元素,从而导致自动布局问题。

文档对这一点非常具体:

请勿使用此属性更改播放的可见性 播放器视图控制器在屏幕上时进行控制,因为这样做 所以创建或销毁 UI 元素。

链接: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/#//apple_ref/occ/instp/AVPlayerViewController/showsPlaybackControls

【讨论】:

@Abid 我面临同样的问题。你有什么解决办法吗? 我创建了一个 AVPlayerViewController 实例并将其添加到视图中,如下所示: moviePlayer = AVPlayerViewController.init() moviePlayer?.allowsPictureInPicturePlayback = false moviePlayer?.showsPlaybackControls = true moviePlayer?.player = AVPlayer.init( url: item!.mediaURL!) moviePlayer?.view.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(moviePlayer!.view) moviePlayer!.view.pinAllAnchors(to: contentView)返回。

以上是关于AVPlayerController:全屏自动布局问题的主要内容,如果未能解决你的问题,请参考以下文章

如何设置自动布局使图像占据全屏?

使用自动布局使 UIView 全屏

将 ViewController 作为 Overlay 嵌入到 AVPlayerController 中

div+css如何实现全屏放大键效果加自动播放语音效果?

iOS 系统根据导航栏和状态栏自动修改布局

自动布局子视图在 Swift 4 中添加问题