我想在AVPlayer
中获取视频的位置x,y,在AVPlayerLayer
中获取显示视频的高度,宽度。
我的代码是:
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];
最佳答案
你可以这样
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;
}
关于ios - 我如何在 objective-c 中的AVPlayerLayer中获得AVPlayer y的位置,高度,宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45231101/