9

我正在使用 AVCaptureAudioDataOutputSampleBufferDelegate 捕获音频

  _captureSession = [[AVCaptureSession alloc] init];
  [self.captureSession setSessionPreset:AVCaptureSessionPresetLow];


  // Setup Audio input
  AVCaptureDevice *audioDevice = [AVCaptureDevice
                                defaultDeviceWithMediaType:AVMediaTypeAudio];
  AVCaptureDeviceInput *captureAudioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
  if(error){
      NSLog(@"Error Start capture Audio=%@", error);
  }else{
      if ([self.captureSession canAddInput:captureAudioInput]){
          [self.captureSession addInput:captureAudioInput];
      }
  }


  // Setup Audio output
  AVCaptureAudioDataOutput *audioCaptureOutput = [[AVCaptureAudioDataOutput alloc] init];
  if ([self.captureSession canAddOutput:audioCaptureOutput]){
      [self.captureSession addOutput:audioCaptureOutput];
  }
  [audioCaptureOutput release];


  //We create a serial queue 
  dispatch_queue_t audioQueue= dispatch_queue_create("audioQueue", NULL);
  [audioCaptureOutput setSampleBufferDelegate:self queue:audioQueue];
  dispatch_release(audioQueue);


  /*We start the capture*/
  [self.captureSession startRunning];

代表:

  - (void)captureOutput:(AVCaptureOutput *)captureOutput  didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

  // do something with sampleBuffer
  }

问题是如何从 sampleBuffer 播放音频?

4

1 回答 1

0

您可以使用以下代码创建NSDataCMSampleBufferRef然后使用 AVAudioPlayer 播放。

- (void)captureOutput:(AVCaptureOutput *)captureOutput  didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

    AudioBufferList audioBufferList;
    NSMutableData *data= [NSMutableData data];
    CMBlockBufferRef blockBuffer;
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);

    for( int y=0; y< audioBufferList.mNumberBuffers; y++ ){

        AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
        Float32 *frame = (Float32*)audioBuffer.mData;

        [data appendBytes:frame length:audioBuffer.mDataByteSize];

    }

    CFRelease(blockBuffer);

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil];
    [player play];
}

不过,我担心这将如何提高性能。可能有更好的方法来做你想要完成的事情。

于 2012-10-30T17:52:27.373 回答