本文介绍了AVKit.AVPlayerViewController-控件在iOS11及更高版本中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个带控件的videoPlayer.这些在iOS11 +中不可见直到iOS10都没问题

I have a videoPlayer with controls.These are not visible with iOS11 +no problem up to iOS10

这是使用 iPhone8 模拟器

iPhone 8演示

这是使用 iPhoneX 模拟器的演示.控件未显示在播放器底部

This is a demo with the iPhoneX simulator. The controls aren't showing at the bottom of the player

iPhone X演示

我认为这与新的安全区域有很大关系.我们正在使用 contentOffset (如下)从顶部开始偏移.更改值没有区别. 看不到任何控件

I believe this has a lot to do with the new safe area. We are using contentOffset (below) to offset from the top. Changing the value makes no difference. No controls visible

这是我用来配置播放器的以下代码:

This is the code below I am using to configure the player :

let statusBarHeight: CGFloat = 20
let contentOffset: CGFloat = 50

func setupPlayerView() {
        containerView.addSubview(moviePlayerController.view)
        moviePlayerController.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspect.rawValue)
        moviePlayerController.view.sizeToFit()
        moviePlayerController.showsPlaybackControls = true

        moviePlayerController.view.translatesAutoresizingMaskIntoConstraints = false

        if #available(iOS 11.0, *) {
            moviePlayerController.view.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: contentOffset).isActive = true
        } else {
            moviePlayerController.view.topAnchor.constraint(equalTo: topAnchor, constant: statusBarHeight + contentOffset).isActive = true
        }
        NSLayoutConstraint.activate([
            moviePlayerController.view.bottomAnchor.constraint(equalTo: bottomAnchor),
            moviePlayerController.view.leftAnchor.constraint(equalTo: leftAnchor),
            moviePlayerController.view.rightAnchor.constraint(equalTo: rightAnchor)
        ])
    }

showsPlaybackControls设置为True,但这是默认设置

showsPlaybackControls is set to True, this is the default though

是否有人遇到过此问题?

推荐答案

您的代码始终是错误的.即使在iOS 10上,控件也位于屏幕底部,离电影很远.正确的方法是获取视频轨道的naturalSize并使用它配置播放器视图的纵横比,以便播放器视图等于电影的大小.现在,控件将出现在影片的底部,而不是屏幕的底部.

Your code was always wrong. Even on iOS 10 the controls are way down at the bottom of the screen, far from the movie. The correct approach is to get the video track’s naturalSize and use it to configure the aspect ratio of the player view, so that the player view is the size of the movie. Now the controls will appear at the bottom of the movie, not the bottom of the screen.

另一个问题是,您所做的事情完全是非法的.您不能只制作一个AVPlayerViewController,获取视图控制器的视图,然后使用addSubview将视图的内容填充到接口中.为了使视图控制器成为self子视图控制器(当前的视图控制器,您必须执行一次严格的舞蹈,而您没有在做这舞蹈.工作示例:

Another problem is that what you're doing is totally illegal. You cannot just make an AVPlayerViewController, grab the view controller's view and stuff that view into the interface with addSubview. There is a rigorous dance that you are required to do in order to make the view controller a child view controller of self (the current view controller — and you are not doing the dance. Here is a working example:

    let url = // whatever
    let player = AVPlayer(url:url)
    let av = AVPlayerViewController()
    av.player = player
    av.view.frame = // whatever
    self.addChild(av) // <—- LOOK!
    self.view.addSubview(av.view)
    av.didMove(toParent:self) // <— LOOK!

(请注意,这是Swift 4.2.)

(Note that this is Swift 4.2.)

这篇关于AVKit.AVPlayerViewController-控件在iOS11及更高版本中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:21