将CMSampleBufferRef数据传递到音频输出插孔

时间:2013-03-19 18:22:09

标签: ios avfoundation core-audio

我正在开发一个应用程序,我需要通过输出音频插孔同时通过音频捕获记录并保存视频。

我已经研究了aurio touch apple示例代码并实现了音频直通。

我还通过AVCaptureSession实施了视频录制。  以上两个功能单独完成并且完美地工作。

但是当我合并功能时,由于AVCapturesession的音频会话,音频传递不起作用。

我还试图通过AVCaptureSession委托方法传递音频数据。以下是我的代码:

OSStatus err = noErr;


AudioBufferList audioBufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
CMItemCount numberOfFrames = CMSampleBufferGetNumSamples(sampleBuffer); // corresponds to the number of CoreAudio audio frames

currentSampleTime += (double)numberOfFrames;

AudioTimeStamp timeStamp;
memset(&timeStamp, 0, sizeof(AudioTimeStamp));
timeStamp.mSampleTime = currentSampleTime;
timeStamp.mFlags |= kAudioTimeStampSampleTimeValid;

AudioUnitRenderActionFlags flags = 0;
aurioTouchAppDelegate *THIS = (aurioTouchAppDelegate *)[[UIApplication sharedApplication]delegate];
 err = AudioUnitRender(self.rioUnit, &flags, &timeStamp, 1, numberOfFrames, &audioBufferList);

if (err) { printf("PerformThru: error %d\n", (int)err); }

但它给出了错误。请告知我们可以尽快做些什么。我已经查看了很多文档和如此多的代码,但找不到任何解决方案。请帮忙..

1 个答案:

答案 0 :(得分:0)

这是一些更好的错误处理代码。它返回什么错误?您可以通过在文档中搜索错误说明来查找错误说明。

static void CheckError (OSStatus error, const char *operation) {
    if (error == noErr) return;

    char str[20] = {};
    // see if it appears to be a 4 char code
    *(UInt32*)(str + 1) = CFSwapInt32HostToBig(error);
    if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
        str[0] = str[5] = '\'';
        str[6] = '\0';
    } else {
        sprintf(str, "%d", (int)error);
    }

    fprintf(stderr, "Error: %s(%s)\n", operation, str);
    exit(1);
}

- (void)yourFunction
{
    AudioBufferList audioBufferList;
    CMBlockBufferRef blockBuffer;
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
    CMItemCount numberOfFrames = CMSampleBufferGetNumSamples(sampleBuffer); // corresponds to the number of CoreAudio audio frames

    currentSampleTime += (double)numberOfFrames;

    AudioTimeStamp timeStamp;
    memset(&timeStamp, 0, sizeof(AudioTimeStamp));
    timeStamp.mSampleTime = currentSampleTime;
    timeStamp.mFlags |= kAudioTimeStampSampleTimeValid;

    AudioUnitRenderActionFlags flags = 0;
    aurioTouchAppDelegate *THIS = (aurioTouchAppDelegate *)[[UIApplication sharedApplication]delegate];
    CheckError(AudioUnitRender(self.rioUnit, &flags, &timeStamp, 1, numberOfFrames, &audioBufferList),
               "Error with AudioUnitRender");
}