1

我正在尝试全屏播放电影,其中 AVCaptureSession 在角落的较小视图中显示前置摄像头(想想 FaceTime)。如果我注释掉播放视频的代码,FrontCamera 会完美地显示在我放置包含它的 UIView 的角落。如果我让代码按原样运行,则只显示视频,覆盖包含 AVCapture 的 subLayer 的 UIView。另一个子问题是电影的控件和时间线栏显示,我想知道是否有办法在 MPMoviePlayerController 中禁用它。这是我正在使用的代码:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [self frontFacingCameraIfAvailable];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

[session startRunning];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *proud = [[documentsDirectoryPath stringByAppendingPathComponent:@"proud"] stringByAppendingPathComponent:selectedCountry];

NSURL  *movieURL = [[NSURL fileURLWithPath:proud] retain];
self.player =

[[MPMoviePlayerController alloc] initWithContentURL: movieURL];

[player prepareToPlay];

player.allowsAirPlay = NO;
player.scalingMode = MPMovieScalingModeFill;    
self.player.view.frame = self.view.frame;

[self.view addSubview: player.view];
[self.player setFullscreen:YES animated:YES];

// ...

[[NSNotificationCenter defaultCenter] 
 addObserver:self
 selector:@selector(movieFinishedCallback:)
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player];


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayerWillExitFullscreen:)
                                             name:MPMoviePlayerWillExitFullscreenNotification
                                           object:player];


[player play];

vImagePreview 是在标头中声明的 IBOutlet UIView 属性。

4

1 回答 1

1

默认情况下,视图将显示最后添加的视图。所以当代码添加电影播放器​​视图时:

[self.view addSubview:player.view];

它位于其他所有内容之上,并且具有父级边界的完整大小的框架。如果您想保留某些子视图,则可以在添加电影播放器​​后将其放回顶部...

[self.view bringSubviewToFront:otherSubview];

..或将电影播放器​​放在它下面开始...

[self.view insertSubview:player.view belowSubview:otherSubview];

还有很多其他方法可用于控制视图层次结构。希望有帮助。

于 2012-04-24T04:19:55.703 回答