2013-03-19 80 views
13

我正在開發一款應用程序,在該應用程序中,我需要通過輸出音頻插孔同時錄製和保存視頻來傳遞音頻。將CMSampleBufferRef數據傳遞到音頻輸出插孔

我已經看着aurio觸摸蘋果示例代碼並實現了音頻直通。

我也通過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); } 

但它給錯誤。請告知可以儘快做些什麼。我查閱了很多文檔和許多代碼,但找不到任何解決方案。請幫忙..

回答

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"); 
}