Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change video orientation of AVPlayer?

I want to rotate the replay of a video using AVPlayer. Is there a way to rotate it 90 degrees clockwise?
Here's some code:

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()

UPDATE

This one works:

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()
like image 451
mafioso Avatar asked Aug 03 '16 06:08

mafioso


2 Answers

Hooni answer in Swift 3 :

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

func degreeToRadian(_ x: CGFloat) -> CGFloat {
    return .pi * x / 180.0
}
like image 165
Maor Avatar answered Oct 29 '22 17:10

Maor


If you are using Objective-C, the code below will help.

Sample code:

#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))];
}
like image 22
hooni Avatar answered Oct 29 '22 19:10

hooni