我找不到在iPhoneX中显示AVPlayerViewController
控件的解决方案。通过使用AVPlayerViewController.gravity = .aspectfill
可以显示控制权,但视频会填满整个屏幕并剪辑其余部分。任何想法??这是我的代码:
func loadVideoURL () {
let fileURL = URL.init(string: (exerciseDetailsObj?.video_file)!)
avPlayer = AVPlayer(url: fileURL!)
let playerController = AVPlayerViewController()
playerController.player = avPlayer
playerController.view.frame = CGRect(x: 0, y: 0, width:viewPlayerContainer.bounds.size.width, height: viewPlayerContainer.bounds.size.height)
playerController.showsPlaybackControls = false
avPlayerController = playerController
self.addChild(avPlayerController)
viewPlayerContainer.addSubview(avPlayerController.view)
viewPlayerContainer.sendSubviewToBack(avPlayerController.view)
avPlayerController.didMove(toParent: self)
avPlayerController.addObserver(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil)
}
}
func enterFullscreen(playerViewController: AVPlayerViewController) {
playerViewController.showsPlaybackControls = true //not dispalyed in iphonX
let selectorName: String = {
if #available(iOS 11.3, *) {
return "_transitionToFullScreenAnimated:interactive:completionHandler:"
} else if #available(iOS 11, *) {
return "_transitionToFullScreenAnimated:completionHandler:"
} else {
return "_transitionToFullScreenViewControllerAnimated:completionHandler:"
}
}()
let selectorToForceFullScreenMode = NSSelectorFromString(selectorName)
if playerViewController.responds(to: selectorToForceFullScreenMode) {
playerViewController.perform(selectorToForceFullScreenMode, with: true, with: nil)
}
}
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if keyPath == "videoBounds"
{
if let rect = change?[.newKey] as? NSValue
{
if let newrect = rect.cgRectValue as CGRect?
{
if newrect.size.height <= 214
{
avPlayerController.showsPlaybackControls = false
}
}
}
}
}
答案 0 :(得分:1)
检查一下。参考代码,并相应地更改为您的代码。
@IBOutlet weak var videoPlayerView: UIView! // Take UIview on storyboard on which you want to display video
var player: AVPlayer?
var playerController = AVPlayerLayer()
override func viewDidLoad()
{
self.videoPlayerView.layer.insertSublayer(playerController, at: 0)
player = AVPlayer(url: videoUrl!)
playerController.player = player
self.player?.play()
}
override func viewDidLayoutSubviews() {
playerController.videoGravity = AVLayerVideoGravity(rawValue: AVLayerVideoGravity.resizeAspectFill.rawValue)
playerController.frame = self.videoPlayerView.bounds
}