AVCaptureSession并检测来自麦克风的打击

时间:2013-05-20 07:11:07

标签: iphone ios objective-c avcapturesession

  

我正在尝试从avcapture会话录制视频,同时我正在检测打击但遇到问题,当我点击录制按钮然后相机被冻结。,一个重要的事情是当我从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];


    }
  
    
      

和开始录制的代码是[self toggleRecording:nil];如下

    
  
- (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];
    }
  }
  
    
      
        

我正在设置avcapture会话如下

      
    
  
  - (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;
  }

1 个答案:

答案 0 :(得分:0)

  

我用谷歌搜索并从这里找到解决方案。   Using AVCaptureSession and AVAudioPlayer together