swift AVPlaye禁止视频自动旋转
时间: 2023-07-13 15:22:04 浏览: 65
要禁止 Swift AVPlayer 视频自动旋转,你可以在你的 View Controller 中实现以下方法:
```swift
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { _ in
if let playerLayer = self.playerView.playerLayer {
let deviceOrientation = UIDevice.current.orientation
switch deviceOrientation {
case .portrait:
playerLayer.connection?.videoOrientation = .portrait
case .landscapeLeft:
playerLayer.connection?.videoOrientation = .landscapeLeft
case .landscapeRight:
playerLayer.connection?.videoOrientation = .landscapeRight
default:
break
}
}
}, completion: nil)
}
```
这段代码会在设备方向改变时被调用,然后可以手动设置 playerLayer 的 videoOrientation,从而禁止视频自动旋转。上面的代码假设你有一个 playerView 属性可以访问你的 AVPlayerLayer。如果你没有 playerView 属性,你可以将代码中的 `self.playerView.playerLayer` 替换为你的 AVPlayerLayer 对象。