AVFoundation屏幕记录问题

时间:2013-06-18 17:47:27

标签: objective-c avfoundation

您好我正在尝试使用AVFoundation框架来制作屏幕。 我遇到了两个问题: 1)按下按钮后,录制开始延迟。 2)如果我插入断点以抛出所有异常,它会卡在:

[session startRunning];

代码是:

-(void)initAVRecord{
    session = [[AVCaptureSession alloc]init];
    if ([session canSetSessionPreset:AVCaptureSessionPresetHigh]){
        session.sessionPreset = AVCaptureSessionPresetHigh;
    }
    display = CGMainDisplayID();
    captureScreenInput = [[AVCaptureScreenInput alloc]initWithDisplayID:display];
    if ([session canAddInput:captureScreenInput]){
        [session addInput:captureScreenInput];
        NSLog(@"screen device added to session ");
    }else{
        NSLog(@"no screen device added to session");
    }

    audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    NSError *error;
    captureAudioInput = [[AVCaptureDeviceInput alloc]initWithDevice:audioDevice error:&error];
    if ([session canAddInput:captureAudioInput]){
        [session addInput:captureAudioInput];
        NSLog(@"audio device added to session ");
    }else{
        NSLog(@" no audio device added");
    }

    movieOutput = [[AVCaptureMovieFileOutput alloc]init];
    [movieOutput setDelegate:self];
    if ([session canAddOutput:movieOutput]){
        [session addOutput:movieOutput];
    }
    [session startRunning];
}


- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
    [[NSWorkspace sharedWorkspace] openURL:outputFileURL];
}

-(BOOL)captureOutputShouldProvideSampleAccurateRecordingStart:(AVCaptureOutput *)captureOutput{
    return NO;
}



- (IBAction)recordButton:(id)sender {
    if ([[recordButton title] isEqualToString:@"Record"]){
        [recordButton setTitle:@"Stop"];
        NSLog(@"Minimum Frame Duration: %f, Crop Rect: %@, Scale Factor: %f, Capture Mouse Clicks: %@, Capture Mouse Cursor: %@, Remove Duplicate Frames: %@",
              CMTimeGetSeconds([self.captureScreenInput minFrameDuration]),
              NSStringFromRect(NSRectFromCGRect([self.captureScreenInput cropRect])),
              [self.captureScreenInput scaleFactor],
              [self.captureScreenInput capturesMouseClicks] ? @"Yes" : @"No",
              [self.captureScreenInput capturesCursor] ? @"Yes" : @"No",
              [self.captureScreenInput removesDuplicateFrames] ? @"Yes" : @"No");
        char *tempNameBytes = tempnam([[@"~/Desktop/" stringByStandardizingPath]fileSystemRepresentation],"screenRec1_");
        NSString *tempName = [[NSString alloc]initWithBytesNoCopy:tempNameBytes length:strlen(tempNameBytes) encoding:NSUTF8StringEncoding freeWhenDone:YES];
        [movieOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:[tempName stringByAppendingPathExtension:@"mov"]] recordingDelegate:self];

    }else if ([[recordButton title] isEqualToString:@"Stop"]){
        [recordButton setTitle:@"Record"];
        [movieOutput stopRecording];
    }

}

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:0)

  

startRunning()方法是一个阻塞调用,可能需要一些时间,因此您应该在串行队列上执行会话设置,以便不阻止主队列(这使UI保持响应)。有关实施示例,请参阅AVCam-iOS: Using AVFoundation to Capture Images and Movies

来自Apple doc startrunning