AVPlayerを使用してビデオの再生を回転させたい。時計回りに90度回転させる方法はありますか?
ここにいくつかのコードがあります:
self.player = AVPlayer(URL: NSURL(fileURLWithPath: dataPath))
playerLayer = AVPlayerLayer.init(player: self.player)
playerLayer.frame = view.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(playerLayer)
player.play()
更新
これは機能します:
self.player = AVPlayer(URL: NSURL(fileURLWithPath: dataPath))
playerLayer = AVPlayerLayer.init(player: self.player)
playerLayer.setAffineTransform(CGAffineTransformMakeRotation(CGFloat(M_PI))
playerLayer.frame = view.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(playerLayer)
player.play()
Objective-Cを使用している場合は、以下のコードが役立ちます。
#define degreeToRadian(x) (M_PI * x / 180.0)
#define radianToDegree(x) (180.0 * x / M_PI)
- (void)rotateVideoPlayerWithDegree:(CGFloat)degree {
[_playerLayer setAffineTransform:CGAffineTransformMakeRotation(degreeToRadian(degree))];
}
Swift 3のHooniの回答:
let affineTransform = CGAffineTransform(rotationAngle: degreeToRadian(90))
avPlayerLayer.setAffineTransform(affineTransform)
func degreeToRadian(_ x: CGFloat) -> CGFloat {
return .pi * x / 180.0
}