我有一个按需播放视频片段的应用。这在以前版本的Xcode中运行良好,但我已升级到8.3.3并且遇到AVPlayerViewController问题。
视频正常播放和显示。控制栏出现在视图的底部,但不响应点击,一旦淡出,在点击视频时不再出现 - 除非,即我点击视图的左上角附近。
我的猜测是,实际的控件隐藏在某种覆盖层中,这种覆盖层的大小不正确,即它没有正确覆盖整个视频视图。有没有办法强制AVPlayerViewController重新布局?
我尝试添加:
_playerController!.view.setNeedsLayout()
_playerController!.view.layoutIfNeeded()
但这没有效果。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
_player = AVPlayer()
_playerController = AVPlayerViewController()
_playerController!.showsPlaybackControls = true
_playerController!.allowsPictureInPicturePlayback = false
_playerController!.videoGravity = AVLayerVideoGravityResizeAspectFill
_playerController!.player = _player
self.addChildViewController(_playerController!)
videoView.addSubview(_playerController!.view)
...
override func viewDidLayoutSubviews() {
let frame = UIScreen.main.bounds
_vidWidth = frame.width - 256.0
_vidHeight = _vidWidth / 854.0 * 480.0
videoView.frame = CGRect(x: 0, y: 10.0, width: _vidWidth, height: _vidHeight)
videoView.backgroundColor = UIColor.black
_playerController?.view.frame = CGRect(x: 0, y: 0, width: _vidWidth, height: _vidHeight)
...
func playVideo(_ clip: Clip) {
var videoUrl: URL? = nil
if clip.offlineLocation != nil && clip.state == 2 {
_videoPath = clip.offlineLocation!
videoUrl = URL(fileURLWithPath: _videoPath!)
}
else {
_videoPath = "https://xxxx.cloudfront.net/" + clip.location
videoUrl = URL(string: _videoPath!)
}
NSLog(_videoPath!)
let asset = AVURLAsset(url:videoUrl!, options:nil)
let playerItem = AVPlayerItem(asset: asset)
_player!.replaceCurrentItem(with: playerItem)
_player!.play()
}