正如问题所述,有没有办法在avplayer中旋转视频。
目前,我正在以模态方式呈现一个avplayercontroller实例的类。整个应用程序仅在纵向模式下运行,但我希望此视频以横向模式播放。
我已经尝试过" shouldAutorotate"以及此处列出的其他方法: Only ONE VIEW landscape mode
Swift似乎并不希望允许模态呈现的视图控制器成为不同的方向。
我的视频控制器类的代码
import UIKit
import AVFoundation
import AVKit
class streamStuffViewController: AVPlayerViewController {
var isPresented = true
override func viewDidLoad() {
super.viewDidLoad()
self.player = AVPlayer(URL: NSURL(string: currentStream))
self.player.play()
// Do any additional setup after loading the view.
}
override func viewWillDisappear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidDisappear(animated: Bool) {
self.player.pause()
}
}
答案 0 :(得分:0)
我刚才也有同样的情况。我也试图改变方向只有一个屏幕,我做了一个黑客我添加
self.view.transform = CGAffineTransformMakeRotation(1.5708)
viewDidLoad()
中的这一行用于旋转整个视图。但是,您仍然会在纵向模式下看到载波和电池符号。
答案 1 :(得分:0)
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is PlayVideoViewController {
return UIInterfaceOrientationMask.all
} else {
return UIInterfaceOrientationMask.portrait
}
}
return UIInterfaceOrientationMask.portrait
}
如果您的应用程序仅在potrait模式下工作,但您只希望avplayer旋转,则将此代码添加到AppDelegate.swift文件中,并用ControllerName替换“ PlayVideoViewController”。
答案 2 :(得分:0)
我正在听方向改变:
// view did load
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
然后我旋转视频播放器,但在我的场景中只能左右旋转:
@objc func deviceOrientationDidChange() {
switch UIDevice.current.orientation {
case .faceUp, .faceDown, .portrait, .unknown, .portraitUpsideDown:
// default the player to original rotation
self.playerController.view.transform = .identity
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
case .landscapeLeft:
self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((90 * Double.pi)/180))
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
case .landscapeRight:
self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180))
self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
}
}
如果您喜欢动画,可以将代码包装在UIView.animate
中。
别忘了在NotificationCenter.default.removeObserver(self)
上致电deinit()