1

您好我正在尝试使用 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];
    }

}

有人可以告诉我我做错了什么吗?

4

1 回答 1

0

startRunning() 方法是一个阻塞调用,可能需要一些时间,因此您应该在串行队列上执行会话设置,以免主队列被阻塞(这使 UI 保持响应)。有关实现示例,请参阅AVCam-iOS:使用 AVFoundation 捕获图像和电影。

来自 Apple doc startrunning

于 2017-08-17T02:48:35.137 回答