Reputation: 1632
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()
Upvotes: 17
Views: 14242
Reputation: 1
I do,
+ (Class)layerClass
{
return [AVPlayerLayer class];
}
Then in init, the class is an NSView:
[(AVPlayerLayer*)self.layer setPlayer:player];
self.wantsLayer=YES;
self.superview.wantsLayer=YES;
if (fakePortrait) {
[self rotateByAngle:90];
}
However, this only works in 10.13. I'm looking for a solution for 10.9 and above. If I rotate the video pre 10.13, it disappears.
Upvotes: -2
Reputation: 3430
Hooni answer in Swift 3 :
let affineTransform = CGAffineTransform(rotationAngle: degreeToRadian(90))
avPlayerLayer.setAffineTransform(affineTransform)
func degreeToRadian(_ x: CGFloat) -> CGFloat {
return .pi * x / 180.0
}
Upvotes: 8
Reputation: 249
If you are using Objective-C, the code below will help.
#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))];
}
Upvotes: 6