Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone AVCapture会话和检测话筒的打击_Iphone_Ios_Objective C_Avcapturesession - Fatal编程技术网

Iphone AVCapture会话和检测话筒的打击

Iphone AVCapture会话和检测话筒的打击,iphone,ios,objective-c,avcapturesession,Iphone,Ios,Objective C,Avcapturesession,我试图从avcapture会话录制视频,同时检测到打击,但当我单击“录制”按钮,然后相机冻结时遇到问题。一个重要的问题是,当我从avcapture会话中删除音频输入时,一切正常。我该如何处理这一问题。任何帮助都是值得感激的 录制按钮的代码如下所示 以及开始录制的代码,即[自切换录制:nil];详情如下: 我将avcapture会话设置如下 我已经在谷歌上搜索并从这里找到了解决方案。 [recordButton setEnabled:NO]; NSURL *url = [NSURL fileU

我试图从avcapture会话录制视频,同时检测到打击,但当我单击“录制”按钮,然后相机冻结时遇到问题。一个重要的问题是,当我从avcapture会话中删除音频输入时,一切正常。我该如何处理这一问题。任何帮助都是值得感激的

录制按钮的代码如下所示

以及开始录制的代码,即[自切换录制:nil];详情如下:

我将avcapture会话设置如下

我已经在谷歌上搜索并从这里找到了解决方案。

 [recordButton setEnabled:NO];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else{
    NSLog(@"error%@",[error description]);
    }
       [recordButton setImage:[UIImage imageNamed:@"record-h.png"] forState:UIControlStateNormal];

        [timerLbl setHidden:false];

        [lblHr setHidden:false];
        [lblMnt setHidden:false];
        [self toggleRecording:nil];
        startDate = [NSDate date] ;

        startDate = [[startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))] retain];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];


    }
- (IBAction)toggleRecording:(id)sender
   {
     // Start recording if there isn't a recording running. Stop recording if there is.
      if ([UIImagePickerController isSourceTypeAvailable:SOURCETYPE]) { 
      if (![[[self captureManager] recorder] isRecording])
       [[self captureManager] startRecording];
     else
    [[self captureManager] stopRecording];
    }
  }
  - (BOOL) setupSession
   {
     BOOL success = NO;

// Set torch and flash mode to auto
if ([[self backFacingCamera] hasFlash]) {
    if ([[self backFacingCamera] lockForConfiguration:nil]) {
        if ([[self backFacingCamera] isFlashModeSupported:AVCaptureFlashModeAuto]) {
            [[self backFacingCamera] setFlashMode:AVCaptureFlashModeAuto];
        }
        [[self backFacingCamera] unlockForConfiguration];
    }
}
if ([[self backFacingCamera] hasTorch]) {
    if ([[self backFacingCamera] lockForConfiguration:nil]) {
        if ([[self backFacingCamera] isTorchModeSupported:AVCaptureTorchModeAuto]) {
            [[self backFacingCamera] setTorchMode:AVCaptureTorchModeAuto];
        }
        [[self backFacingCamera] unlockForConfiguration];
    }
}

// Init the device inputs
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontFacingCamera] error:nil];
AVCaptureDeviceInput *newAudioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioDevice] error:nil];


// Setup the still image file output
AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                AVVideoCodecJPEG, AVVideoCodecKey,
                                nil];
[newStillImageOutput setOutputSettings:outputSettings];
[outputSettings release];


// Create session (use default AVCaptureSessionPresetHigh)
AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];


// Add inputs and output to the capture session
if ([newCaptureSession canAddInput:newVideoInput]) {
    [newCaptureSession addInput:newVideoInput];
}
if ([newCaptureSession canAddInput:newAudioInput]) {
    [newCaptureSession addInput:newAudioInput];
}
if ([newCaptureSession canAddOutput:newStillImageOutput]) {
    [newCaptureSession addOutput:newStillImageOutput];
}

[self setStillImageOutput:newStillImageOutput];
[self setVideoInput:newVideoInput];
[self setAudioInput:newAudioInput];
[self setSession:newCaptureSession];

[newStillImageOutput release];
[newVideoInput release];
[newAudioInput release];
[newCaptureSession release];

// Set up the movie file output
NSURL *outputFileURL = [self tempFileURL];
AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:outputFileURL];
[newRecorder setDelegate:self];

// Send an error to the delegate if video recording is unavailable
if (![newRecorder recordsVideo] && [newRecorder recordsAudio]) {
    NSString *localizedDescription = NSLocalizedString(@"Video recording unavailable", @"Video recording unavailable description");
    NSString *localizedFailureReason = NSLocalizedString(@"Movies recorded on this device will only contain audio. They will be accessible through iTunes file sharing.", @"Video recording unavailable failure reason");
    NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
                               localizedDescription, NSLocalizedDescriptionKey, 
                               localizedFailureReason, NSLocalizedFailureReasonErrorKey, 
                               nil];
    NSError *noVideoError = [NSError errorWithDomain:@"AVCam" code:0 userInfo:errorDict];
    if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
        [[self delegate] captureManager:self didFailWithError:noVideoError];
    }
}

[self setRecorder:newRecorder];
    [newRecorder release];

    success = YES;

    return success;
  }