如何更改AVPlayer的视频方向?

时间:2016-08-03 06:46:06

标签: ios swift rotation avfoundation orientation

我想使用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()

2 个答案:

答案 0 :(得分:4)

如果您使用的是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))];
}

答案 1 :(得分:3)

Hooni在Swift 3中回答:

let affineTransform = CGAffineTransform(rotationAngle: degreeToRadian(90))
avPlayerLayer.setAffineTransform(affineTransform)

func degreeToRadian(_ x: CGFloat) -> CGFloat {
    return .pi * x / 180.0
}