我正在尝试使用AVFoundation录制视频。当我只将视频输入添加到会话时,一切正常,但是当我向其添加音频输入时,它会停止录制视频。(录制开始后立即调用Delegate方法)。这是我的代码:
-(void) recordVideo
{
self.session = [[AVCaptureSession alloc] init];
if([session canSetSessionPreset:AVCaptureSessionPresetMedium])
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = [self.cameraView layer];
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = viewLayer.bounds;
[viewLayer addSublayer:captureVideoPreviewLayer];
self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:[self frontFacingCameraIfAvailable] error:nil];
self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:[self audioDevice] error:nil];
if(!videoInput)
NSLog(@"Couldn't create input!");
else
{
self.output= [[AVCaptureMovieFileOutput alloc] init];
NSString *pathString = [[self outputPath]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *fileURL = [NSURL fileURLWithPath:pathString];
[session beginConfiguration];
[session removeInput:[self videoInput]];
if([session canAddInput:videoInput])
[session addInput:videoInput];
[videoInput release];
[session removeInput:[self audioInput]];
if([session canAddInput:audioInput])
[session addInput:audioInput];
[audioInput release];
if([session canAddOutput:output])
[session addOutput:output];
[output release];
[session commitConfiguration];
[session startRunning];
[output startRecordingToOutputFileURL:fileURL recordingDelegate:self];
}
- (void) captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections
{
NSLog(@"Recording Started at %@",fileURL);
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections error:(NSError *)error
{
NSLog(@"Recording to file ended");
[session stopRunning];
[session release];
}
- (AVCaptureDevice *)frontFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionBack)
{
captureDevice = device;
break;
}
}
return captureDevice;
}
- (AVCaptureDevice *) audioDevice
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
if ([devices count] > 0) {
return [devices objectAtIndex:0];
}
return nil;
}
我在一段固定时间之后调用[output stopRecording],但是当我添加一个音频输入时,它会记录一个帧并立即调用didFinishRecroding委托方法。
有人能告诉我这段代码有什么问题。
由于
答案 0 :(得分:7)
我已经弄清楚了。我需要类别混合。以下是制作它的代码:
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];
if (setCategoryError) { NSLog(@"%@",[setCategoryError description]); }
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing);