我正在使用AVFoundation创建一个应用程序,它基本上是一个可以播放视频的视频,以及一个显示和录制视频的视图。现在我正在实现所有的代码 viewDidLoad该视图,但我认为可能有更好/更快的方式。
viewDid播放视频的视图
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_2311" ofType:@"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
self.MoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.MoviePlayer.controlStyle = MPMovieControlStyleNone;
[self.MoviePlayer.view setFrame:CGRectMake(192, 20, 640, 480)];
[self.view addSubview:self.MoviePlayer.view];
self.MoviePlayer.useApplicationAudioSession = NO;
[self.MoviePlayer prepareToPlay];
[self.MoviePlayer play];
}
viewDidLoad视图显示和录制视频
- (void)viewDidLoad
{
[super viewDidLoad];
[self.countdown setHidden:YES];
self.df = [[NSDateFormatter alloc] init];
[self.df setDateStyle: NSDateFormatterShortStyle];
[self.df setTimeStyle: NSDateFormatterShortStyle];
NSError *error = nil;
self.session = [[AVCaptureSession alloc] init];
[self.session setSessionPreset:AVCaptureSessionPresetMedium];
[self.session setSessionPreset:AVCaptureSessionPreset640x480];
self.videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:[self frontFacingCamera] error:&error];
[self.session addInput:self.videoInputDevice];
self.movieoutput = [[AVCaptureMovieFileOutput alloc] init];
[self.session addOutput:self.movieoutput];
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
self.audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
if([self.session canAddInput:self.audioInputDevice])
[self.session addInput:self.audioInputDevice];
Float64 TotalSeconds = 300;
int32_t preferredTimeScale = 30;
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
self.movieoutput.maxRecordedDuration = maxDuration;
self.movieoutput.minFreeDiskSpaceLimit = 1024 * 1024;
CALayer *rootLayer = [[self view] layer];
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
[captureVideoPreviewLayer setFrame:CGRectMake(192, 20, 640, 480)];
[rootLayer addSublayer:captureVideoPreviewLayer];
[self.session startRunning];
}