我已经通过avcapturesession实现了视频和音频录制。通常大约需要1到2秒钟才能开始录制。
如果需要更多信息,请随意发表评论。 设置捕获会话执行以下步骤:
开始捕获会话
-(void)setupCaptureSession {
// Set up capture session
captureSession = [AVCaptureSession new];
//add video intput
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoCaptureDevice) {
NSError *error;
videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(!error){
if ([captureSession canAddInput:videoInputDevice]) {
[captureSession addInput:videoInputDevice];
}else{
NSLog(@"Failed to add video input");
}
}else{
NSLog(@"Failed to create video device");
}
}else{
NSLog(@"Failed to create video capture device");
}
// add audio input
AVCaptureDevice *audioCaptureDevie = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevie error:&error];
if (audioInputDevice) {
[captureSession addInput:audioInputDevice];
}
// add outputs
// add video preview layer
NSLog(@"Adding video preview layer");
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession]];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//add movie ouput
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
if ([captureSession canAddOutput:movieFileOutput])
[captureSession addOutput:movieFileOutput];
// set up camera configuration for output connection
[self setUpCameraConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetMedium];
if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480])
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];
//----- DISPLAY THE PREVIEW LAYER -----
//Display it full screen under out view controller existing controls
NSLog(@"Display the preview layer");
CGRect layerRect = [[[self view] layer] bounds];
[_previewLayer setBounds:layerRect];
[_previewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
CGRectGetMidY(layerRect))];
//[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
//We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):
UIView *cameraView = [[UIView alloc] init];
[[self view] addSubview:cameraView];
[self.view sendSubviewToBack:cameraView];
[[cameraView layer] addSublayer:_previewLayer];
//----- START THE CAPTURE SESSION RUNNING -----
[captureSession startRunning];
}