前置摄像头视频录制 iPhone 4?
Posted
技术标签:
【中文标题】前置摄像头视频录制 iPhone 4?【英文标题】:Front Camera Video Recording iPhone 4? 【发布时间】:2010-10-27 06:49:07 【问题描述】:我正在尝试借助我从 iPhone 开发人员程序获得的 WWDC 示例,使用 AVFoundation 框架从 iPhone 4 的前置摄像头录制视频。但我仍然无法让它工作......视频没有被录制或可能保存在我的 iPhone 库中......这是我正在尝试使用的代码......如果有人能帮助我解决这个问题,那将非常有帮助我有吗??
-(void)recordVideo
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil];
AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session addInput:videoInput];
[session addOutput:movieFileOutput];
[movieFileOutput release];
if (![session isRunning])
[self performSelector:@selector(startRecording) withObject:nil afterDelay:1.0];
[session startRunning];
- (void) startRecording
NSLog(@"start recording");
AVCaptureConnection *videoConnection = [playVideo connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:[self orientation]];
[[self movieFileOutput] startRecordingToOutputFileURL:[self tempFileURL]
recordingDelegate:self];
- (void) stopRecording
NSLog(@"stop recording");
[[self movieFileOutput] stopRecording];
- (NSURL *) tempFileURL
NSLog(@"temp file url");
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
NSLog(@"exists");
[outputPath release];
return [outputURL autorelease];
- (void) setConnectionWithMediaType:(NSString *)mediaType enabled:(BOOL)enabled;
[[playVideo connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]] setEnabled:enabled];
+ (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections;
NSLog(@"connection with media type");
for ( AVCaptureConnection *connection in connections )
for ( AVCaptureInputPort *port in [connection inputPorts] )
if ( [[port mediaType] isEqual:mediaType] )
return [[connection retain] autorelease];
return nil;
@implementation recordVideo (Internal)
- (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
if ([device position] == position)
return device;
return nil;
- (AVCaptureDevice *) backFacingCamera
NSLog(@"back");
return [self cameraWithPosition:AVCaptureDevicePositionBack];
- (AVCaptureDevice *) frontFacingCamera
NSLog(@"front ");
return [self cameraWithPosition:AVCaptureDevicePositionFront];
@end
@implementation recordVideo (AVCaptureFileOutputRecordingDelegate)
- (void) captureOutput:(AVCaptureFileOutput *)captureOutput
didStartRecordingToOutputFileAtURL:(NSURL *)fileURL
fromConnections:(NSArray *)connections
NSLog(@"did start recording");
- (void) captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
NSLog(@"did finish recording output file");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL, NSError *error)
if (error && [delegate respondsToSelector:@selector(assetLibraryError:forURL:)])
[delegate assetLibraryError:error forURL:assetURL];
];
else
[library release];
@end
【问题讨论】:
【参考方案1】:您正在丢弃错误 - 如果这样做,您将如何调试?
您需要更具体地了解正在发生/未发生的事情。调用了哪些方法? SDK 报告了哪些错误(如果有)?等等……
【讨论】:
【参考方案2】:您已在 -(void)recordVideo 中发布了对 movieFileOutput 的引用; 并且您仍在 - (void) startRecording 中使用已发布的 movieFileOutput 以及 其他功能。你最好不要释放你正在使用的对象。
【讨论】:
以上是关于前置摄像头视频录制 iPhone 4?的主要内容,如果未能解决你的问题,请参考以下文章
Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19