gpt4 book ai didi

ios - AVCaptureMovieFileOutput - 没有事件/启用的连接

转载 作者:技术小花猫 更新时间:2023-10-29 10:14:21 24 4
gpt4 key购买 nike

我正在尝试使用 AVFoundation 在我的 iPhone 应用程序中录制视频。但是每当我单击“录制”按钮时,应用程序就会崩溃并显示此消息

-[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.

我知道在 SO 中提出了同样的问题,但它的答案都没有帮助我。我的问题是相同的代码与另一个应用程序完美配合,当我尝试在此应用程序中使用完全相同的代码时 - 崩溃。但仍然可以正常拍摄照片。

在这里添加我的代码 - 请帮助我,提前致谢

  -(void)viewDidLoad
{
[super viewDidLoad];
self.captureSession = [[AVCaptureSession alloc] init];

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
self.audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];

self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *stillImageOutputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:stillImageOutputSettings];

self.movieOutput = [[AVCaptureMovieFileOutput alloc] init];

[self.captureSession addInput:self.videoInput];
[self.captureSession addOutput:self.stillImageOutput];




previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
UIView *aView = self.view;
previewLayer.frame = CGRectMake(70, 190, 270, 270);
[aView.layer addSublayer:previewLayer];


}




-(NSURL *) tempFileURL
{
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *manager = [[NSFileManager alloc] init];
if ([manager fileExistsAtPath:outputPath])
{
[manager removeItemAtPath:outputPath error:nil];
}


return outputURL;
}

-(IBAction)capture:(id)sender
{

if (self.movieOutput.isRecording == YES)
{
[self.movieOutput stopRecording];
}
else
{
[self.movieOutput startRecordingToOutputFileURL:[self tempFileURL] recordingDelegate:self];
}

}





-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
{


BOOL recordedSuccessfully = YES;
if ([error code] != noErr)
{
id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
if (value)
recordedSuccessfully = [value boolValue];
NSLog(@"A problem occurred while recording: %@", error);
}
if (recordedSuccessfully) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
UIAlertView *alert;
if (!error)
{
alert = [[UIAlertView alloc] initWithTitle:@"Video Saved"
message:@"The movie was successfully saved to you photos library"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
else
{
alert = [[UIAlertView alloc] initWithTitle:@"Error Saving Video"
message:@"The movie was not saved to you photos library"
delegate:nil
cancelButtonTitle:@"OK"

otherButtonTitles:nil, nil];
}

[alert show];
}
];
}

}

最佳答案

我在更改 videoDevice activeFormat 时遇到了同样的问题,后来想录制视频。因为我使用的是质量最好的视频,所以我必须将 sessionPreset 设置为高,如下所示

_session.sessionPreset = AVCaptureSessionPresetHigh;

它对我有用! :)

关于ios - AVCaptureMovieFileOutput - 没有事件/启用的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22110911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com