MPMoviePlayerController涵盖所有其他视图

时间:2012-04-24 04:07:14

标签: iphone xcode mpmovieplayercontroller avcapturesession

我正在尝试全屏播放电影,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属性。

1 个答案:

答案 0 :(得分:1)

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

[self.view addSubview:player.view];

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

[self.view bringSubviewToFront:otherSubview];

..或将电影播放器​​置于其下方以开始......

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

quite a few other methods available来控制视图层次结构。希望有所帮助。