首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVCaptureMovieFileOutput -没有活动/启用连接

AVCaptureMovieFileOutput -没有活动/启用连接
EN

Stack Overflow用户
提问于 2014-03-01 06:15:52
回答 4查看 14.3K关注 0票数 16

我正试图用AVFoundation在我的AVFoundation应用程序中录制视频。但是,每当我单击“记录”按钮时,应用程序就会崩溃。

startRecordingToOutputFileURL:recordingDelegate:-没有活动/启用的连接。

我知道同样的问题是这样问的,但没有一个答案对我有帮助。我的问题是,相同的代码完全适用于另一个应用程序,当我尝试在这个应用程序中使用完全相同的代码时-崩溃。但是照片捕捉仍然很好。

把我的密码加在这里--请帮我,谢谢。

代码语言:javascript
复制
  -(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];
     }
     ];
    }

  }
EN

回答 4

Stack Overflow用户

发布于 2014-11-04 13:32:02

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

_session.sessionPreset = AVCaptureSessionPresetHigh;

它对我有效!:)

票数 11
EN

Stack Overflow用户

发布于 2014-07-10 09:11:20

在您的代码中缺少了几样东西:

  1. 您忘了将movieOutput添加到captureSession
  2. 同样适用于您的audioInput
  3. 所有会话配置都需要由[_captureSession beginConfiguration][_captureSession commitConfiguration]封装。
  4. 对于音频记录,需要将AVAudioSession设置为正确的类别。

下面是更新的代码:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord
                                           error:&error];

    if(!error)
    {
        [[AVAudioSession sharedInstance] setActive:YES error:&error];

        if(error) NSLog(@"Error while activating AudioSession : %@", error);
    }
    else
    {
        NSLog(@"Error while setting category of AudioSession : %@", error);
    }

    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 beginConfiguration];

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

    [self.captureSession commitConfiguration];

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    previewLayer.frame = CGRectMake(0, 0, 320, 500);
    [self.view.layer addSublayer:previewLayer];

    [self.captureSession startRunning];
}

- (IBAction)toggleRecording:(id)sender
{
    if(!self.movieOutput.isRecording)
    {
        [self.recordButton setTitle:@"Stop" forState:UIControlStateNormal];

        NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];

        NSFileManager *manager = [[NSFileManager alloc] init];
        if ([manager fileExistsAtPath:outputPath])
        {
            [manager removeItemAtPath:outputPath error:nil];
        }

        [self.movieOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath]
                                      recordingDelegate:self];
    }
    else
    {
        [self.recordButton setTitle:@"Start recording" forState:UIControlStateNormal];

        [self.movieOutput stopRecording];
    }
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
    NSLog(@"Did finish recording, error %@ | path %@ | connections %@", error, [outputFileURL absoluteString], connections);
}

希望这能有所帮助

票数 9
EN

Stack Overflow用户

发布于 2015-03-25 10:04:15

因为目前您没有一个活动连接记录视频文件。

在记录到输出文件之前,检查连接活动状态:

代码语言:javascript
复制
AVCaptureConnection *c = [self.movieOutput connectionWithMediaType:AVMediaTypeVideo];
if (c.active) {
  //connection is active
} else {
  //connection is not active
  //try to change self.captureSession.sessionPreset,
  //or change videoDevice.activeFormat
}

如果连接未激活,请尝试更改captureSession.sessionPreset或videoDevice.activeFormat。

重复,直到设置了有效的格式(这意味着c.active ==是的)。然后你可以录制视频到输出文件。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22110911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档