Iphone 从AVCaptureAudioDataOutputSampleBufferDelegate播放音频
我正在使用AVCaptureAudioDataOutputSampleBufferDelegate捕获音频Iphone 从AVCaptureAudioDataOutputSampleBufferDelegate播放音频,iphone,objective-c,ios,avfoundation,Iphone,Objective C,Ios,Avfoundation,我正在使用AVCaptureAudioDataOutputSampleBufferDelegate捕获音频 _captureSession = [[AVCaptureSession alloc] init]; [self.captureSession setSessionPreset:AVCaptureSessionPresetLow]; // Setup Audio input AVCaptureDevice *audioDevice = [AVCaptureDevice
_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播放音频?您可以使用以下代码从
CMSampleBufferRef
创建NSData
,然后使用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];
}
-(void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)SampleBufferfromConnection:(AVCaptureConnection*)连接{
AudioBufferList AudioBufferList;
NSMutableData*数据=[NSMutableData];
CMBlockBufferRef块缓冲区;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer,NULL,&audioBufferList,sizeof(audioBufferList),NULL,NULL,0,&blockBuffer);
对于(int y=0;y
不过,我担心这将如何在性能方面发挥作用。也许有更好的方法来完成你想要完成的任务。有一个问题——“玩家”总是为零。你知道吗?输入一个错误,看看是否有错误。还要确保数据不是nilError:Error Domain=NSOSStatusErrorDomain Code=1954115647“操作无法完成。(OSStatus Error 1954115647。)有什么问题吗?你必须添加一个标题,例如AVAudioPlayer的WAV标题才能理解它是一个声音文件。请参阅Selwyn在的回答,了解功能标题
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer
为我返回一个-12737的OSStatus
,根据文档是kCMSampleBufferError\u ArrayTooSmall
。然后代码在appendBytes:length:
上崩溃。有什么想法吗?您好,找到解决方案了吗?如果有,请更新答案。您好,新手,没有,仍然在搜索任何解决方案。我也被这个问题困住了没有,仍然没有任何解决方案