2013-08-19 9 views
6

我使用ExtAudioFileRead函數將音頻文件加載到內存。但是我發現代碼-50總是出現錯誤。這意味着我將錯誤的參數傳遞給函數。但我不知道哪一個是錯誤的參數。iOS中的ExtAudioFileRead下載錯誤代碼-50

音頻文件的數據格式是alac,sampleRate 44100k,有2個通道。

我的代碼如下所示:

ExtAudioFileRef recordFile; 
OSStatus error = noErr; 
error = ExtAudioFileOpenURL((CFURLRef)file, &recordFile); 
checkError(error, "open file"); 

SInt64 frameCount; 
UInt32 size = sizeof(frameCount); 
error = ExtAudioFileGetProperty(recordFile, kExtAudioFileProperty_FileLengthFrames, &size, &frameCount); 
checkError(error, "get frameTotlal"); 

soundStruct *sound = &_sound; 
sound->frameCount = frameCount; 
sound->isStereo = true; 
sound->audioDataLeft = (SInt16 *)calloc(frameCount, sizeof(SInt16)); 
sound->audioDataRight = (SInt16 *)calloc(frameCount, sizeof(SInt16)); 

AudioStreamBasicDescription desc; 
UInt32 descSize = sizeof(desc); 
error = ExtAudioFileGetProperty(recordFile, kExtAudioFileProperty_FileDataFormat, &descSize, &desc); 


[self printASBD:desc]; 

UInt32 channels = desc.mChannelsPerFrame; 

error = ExtAudioFileSetProperty(recordFile, kExtAudioFileProperty_ClientDataFormat, sizeof(inFormat), &inFormat); 

AudioBufferList *bufferList; 
bufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (channels - 1)); 

AudioBuffer emptyBuff = {0}; 
size_t arrayIndex; 
for (arrayIndex = 0; arrayIndex < channels; arrayIndex ++) { 
    bufferList->mBuffers[arrayIndex] = emptyBuff; 
} 

bufferList->mBuffers[0].mData = sound->audioDataLeft; 
bufferList->mBuffers[0].mNumberChannels = 1; 
bufferList->mBuffers[0].mDataByteSize = frameCount * sizeof(SInt16); 
if (channels == 2) { 
    bufferList->mBuffers[1].mData = sound->audioDataRight; 
    bufferList->mBuffers[1].mNumberChannels = 1; 
    bufferList->mBuffers[1].mDataByteSize = frameCount * sizeof(SInt16); 
    bufferList->mNumberBuffers = 2; 
} 

UInt32 count = (UInt32)frameCount; 

error = ExtAudioFileRead(recordFile, &count, bufferList); 
checkError(error, "reading"); // Get a -50 error 

free(bufferList); 
ExtAudioFileDispose(recordFile); 

回答

2

好問題。

當我在ExtAudioFileRead的MONO文件中使用STEREO客戶端數據格式時,發生了此錯誤,請致電ExtAudioFileSetProperty

我不認爲ExtAudioFileRead自動將單聲道文件上轉換爲立體聲文件,如果有不匹配的地方,我認爲它失敗,這個-50錯誤。

要麼使單聲道立體聲文件,設置inFormat.mChannelsPerFrame=1爲單聲道文件。請記住,如果您沒有上轉換,您必須通過從單個單聲道數據中寫入L/R通道來解析audiorender功能中的單聲道文件。