[英]How can i get AVPlayer y position, height, width in AVPlayerLayer in objective c
I want to get position of video in AVPlayer
x,y and height,width of display video in AVPlayerLayer
. 我想在视频的位置AVPlayer
x,y和高度,在显示视频的宽度AVPlayerLayer
。
My code is : 我的代码是:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:path];
AVPlayer *av = [[AVPlayer alloc] initWithURL:url];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:av];
[layer setFrame:self.view.frame];
[self.view.layer addSublayer:layer];
[av play];
You can do like this. 你可以这样
NSString *path = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:path];
av = [[AVPlayer alloc] initWithURL:url];
layer = [AVPlayerLayer playerLayerWithPlayer:av];
[layer setFrame:self.view.frame];
[self.view.layer addSublayer:layer];
[av play];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
CGSize mediaSize = track.naturalSize;
UIInterfaceOrientation orintation = [self orientationForTrack:asset];
if (orintation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"UIInterfaceOrientationLandscapeRight");
float videoWidth = mediaSize.width;
float videoHeight = mediaSize.height;
float calVideoHeight = (videoHeight * ScreenWidth)/videoWidth;
NSLog(@"Video X : 0");
NSLog(@"Video Y : %f",(ScreenHeight/2)-(calVideoHeight/2));
NSLog(@"Video Width : %f",ScreenWidth);
NSLog(@"Video Height : %f",calVideoHeight);
}
else if (orintation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"UIInterfaceOrientationLandscapeLeft");
float videoWidth = mediaSize.width;
float videoHeight = mediaSize.height;
float calVideoHeight = (videoHeight * ScreenWidth)/videoWidth;
NSLog(@"Video X : 0");
NSLog(@"Video Y : %f",(ScreenHeight/2)-(calVideoHeight/2));
NSLog(@"Video Width : %f",ScreenWidth);
NSLog(@"Video Height : %f",calVideoHeight);
}
else if (orintation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
float videoWidth = mediaSize.width;
float videoHeight = mediaSize.height;
float calVideoWidth = (videoWidth * ScreenHeight)/videoHeight;
NSLog(@"Video X : 0");
NSLog(@"Video Y : 0");
NSLog(@"Video Width : %f",calVideoWidth);
NSLog(@"Video Height : %f",ScreenHeight);
}
else if (orintation == UIInterfaceOrientationPortrait) {
NSLog(@"UIInterfaceOrientationPortrait");
float videoWidth = mediaSize.width;
float videoHeight = mediaSize.height;
float calVideoWidth = (videoWidth * ScreenHeight)/videoHeight;
NSLog(@"Video X : 0");
NSLog(@"Video Y : 0");
NSLog(@"Video Width : %f",calVideoWidth);
NSLog(@"Video Height : %f",ScreenHeight);
}
- (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset {
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGSize size = [videoTrack naturalSize];
CGAffineTransform txf = [videoTrack preferredTransform];
if (size.width == txf.tx && size.height == txf.ty)
return UIInterfaceOrientationLandscapeRight;
else if (txf.tx == 0 && txf.ty == 0)
return UIInterfaceOrientationLandscapeLeft;
else if (txf.tx == 0 && txf.ty == size.width)
return UIInterfaceOrientationPortraitUpsideDown;
else
return UIInterfaceOrientationPortrait;
}
You can get video size: 您可以获取视频大小:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:path]
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
CGSize mediaSize = track.naturalSize;
And calculate video position on your screen. 并计算屏幕上的视频位置。
您可以使用AVPlayerLayer
videoRect
属性: https : //developer.apple.com/documentation/avfoundation/avplayerlayer/1385745-videorect
You can add key-value observer to AVPlayerLayer and use the changed value to determine the size & location of video 您可以将键值观察器添加到AVPlayerLayer并使用更改后的值确定视频的大小和位置
in swift 4 迅速4
yourAVPlayerLayer.addObserver(self,
forKeyPath: #keyPath(AVPlayerLayer.videoRect),
options: [.old, .new],
context:nil)
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(AVPlayerLayer.videoRect){
if let size = change?[.newKey] as? CGRect {
print("X:",size.origin.x," Y:",size.origin.y," width:",size.width," height:",size.height);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.