2012-10-10 139 views
1

我試圖打開一個文件並使用ExtAudioFileWrite將數據追加到它。現在,創建/初始寫入(創建時)/轉換工作很好,但不幸的是,我似乎無法打開文件以供後來寫入。只有一個函數似乎打開文件:ExAudioFileOpenURL,但僅用於閱讀。那麼,如果我無法打開文件進行寫入,我該如何在文件末尾編寫代碼?使用ExtAudioFileWrite在文件末尾寫入

+0

你試過'AudioFileOpenURL'然後'ExtAudioFileWrapAudioFileID'嗎? – sbooth

回答

0

我可能是錯的,或不完全理解你的問題。但是,一旦使用ExtAudioFileWrite開始寫入文件,您可以通過再次調用ExtAudioFileWrite來繼續寫入文件。我要冒險進入沒有編譯器的編碼,希望你會得到的想法

例子:

....variables declaration and your logic..... 

//1.Create File to write output 
ExtAudioFileRef outputFile; 

//+++++++ LOOP to append as many files as you want +++++++// 

//2.Create file reference to files you want to merge 
ExtAudioFileRef audioFileObject = 0; 

//3.open the first file 
ExtAudioFileOpenURL (firstFileURL, &audioFileObject); 

//4.get file properties 
AudioStreamBasicDescription AudioFileFormat = 0; 
UInt64 numberOfPacketsToReadInCurrentFile = 0; 
ExtAudioFileGetProperty(audioFileObject, 
         kExtAudioFileProperty_FileLengthFrames, 
         sizeof(numberOfPacketsToReadInCurrentFile), 
         &numberOfPacketsToReadInCurrentFile 
         ); 

ExtAudioFileGetProperty(audioFileObject, 
         kExtAudioFileProperty_FileDataFormat, 
         sizeof(AudioFileFormat), 
         &AudioFileFormat 
         ); 
//5.Set destination ASBD 
ExtAudioFileSetProperty(audioFileObject, 
         kExtAudioFileProperty_ClientDataFormat, 
         sizeof (importFormat), 
         &importFormat 
         ); 

//6.Set/Create/Allocate Some Buffers 
//use AudioFileFormat to setup your buffers accordingly 

AudioBufferList theBufferList = 0 // bufferList setup (I dont remember that) 

//7.Read the file into the buffer 
ExtAudioFileRead(audioFileObject,&numberOfPacketsToReadInCurrentFile,theBufferList); 

//8.Write the buffer to a File  
ExtAudioFileWrite(outputFile, numberOfPacketsToReadInCurrentFile, theBufferList); 
free(theBufferList); 

//if you want to append more files go to point 2 with the next file URL 

//+++++++CLOSE LOOP+++++++++// 

//once you are done.. just dispose the file and clean up everything left 
ExtAudioFileDispose(outputFile); 

這將無法編譯,我猜。但希望能幫助你明白這個想法。

+0

是的,如果你保持文件打開,它會工作得很好。但是,如果關閉它然後再次打開它將從頭開始寫入。我通過直接使用AudioFile解決了問題,我不認爲擴展版本具有此功能。 –

+0

如果你讀到一個緩衝區,就像在你的例子中,內存將在某個時候是一個問題(這是在我的情況) –

+0

就像我說的,我可能不完全理解你需要什麼。我很高興你知道了。對不起,我不能有任何幫助。 – Dan1one