ios使用AVCaptureVideoDataOutput实现连续拍照消除快门声音 两种方法

一.反向声音的方式 ( 连拍会导致有些时候声音不同步,还是会有漏网的快门声 推荐使用第二种方法 第一种方法不适用用于连续抓取照片)

需要导入photoShutter音频声音抵消 不是很靠谱
音频声音下载地址
http://www.cocoachina.com/bbs/job.php?action=download&aid=113986

 - (void)cancelSystermSound {
    static SystemSoundID soundID = 0;
    if (soundID == 0) {
      NSString *path =[[NSBundle bundleWithPath:_bundlepath]pathForResource:@"photoShutter2" ofType:@"caf"];
      NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
      AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
    }
   AudioServicesPlaySystemSound(soundID);
  }

二.AVCaptureVideoDataOutput来对图像进行输出,没有了快门声音,代码如下:

//初始化

  - (void)initAVCaptureSession{

     AVCaptureSession *session = [[AVCaptureSession alloc] init];
     NSError *error;
     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

      //更改这个设置的时候必须先锁定设备,修改完后再解锁,否则崩溃
       [device lockForConfiguration:nil];
       //设置闪光灯为自动
        if ([device isFlashModeSupported:AVCaptureFlashModeOff]) {
         [device setFlashMode:AVCaptureFlashModeOff];
       };

         [device unlockForConfiguration];

        self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
       if (error) {
          NSLog(@"%@",error);
        }

       // Create a VideoDataOutput and add it to the session
        self.imageOutput = [[AVCaptureVideoDataOutput alloc] init];
        [self.session addOutput:self.imageOutput];

        // Configure your output.
        dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
        [self.imageOutput setSampleBufferDelegate:self queue:queue];

         // Specify the pixel format
         self.imageOutput.videoSettings = [NSDictionary dictionaryWithObject:
         [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                            forKey:(id)kCVPixelBufferPixelFormatTypeKey];

          // Start the session running to start the flow of data
           [session startRunning];
          // Assign session to an ivar.
          [self setSession:session];
           if ([self.session canAddInput:self.videoInput]) {
                [self.session addInput:self.videoInput];
            }

          if ([self.session canAddOutput:self.imageOutput]) {
               [self.session addOutput:self.imageOutput];
            }

          AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;
          for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
          if ([d position] == desiredPosition) {
             [self.previewLayer.session beginConfiguration];
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
         for (AVCaptureInput *oldInput in self.previewLayer.session.inputs) {
          
              [[self.previewLayer session] removeInput:oldInput];
        }
         [self.previewLayer.session addInput:input];
         [self.previewLayer.session commitConfiguration];
        break;
          }
      }
}

你可能感兴趣的:(ios使用AVCaptureVideoDataOutput实现连续拍照消除快门声音 两种方法)