2015-02-10 122 views
0

我有一些非常奇怪的東西(至少對我來說,但我是一個noob)。爲什麼AudioFileReadPacketData的參數ioNumBytes會導致崩潰?

UInt32 numBytesReadFromFile; 
OSStatus err = AudioFileReadPacketData(
            audioFile, // The audio file whose audio packets you want to read. 
            NO, // is cache set? 
            &numBytesReadFromFile, // On output, the number of bytes of audio data that were read from the audio file. 
            (AudioStreamPacketDescription *)_packetDescriptions, // array of packet descriptions of data that was read from the audio file (for CBR null) 
            currentPacket, // the next packet to be read into buffer 
            &numPackets, // number of actually read buffers 
            _audioQueueBuffer->mAudioData 
            ); 

AudioFileReadPacketData從音頻文件讀取數據並將其放入緩衝區。

所以我的問題是關於參數numBytesReadFromFile。 Apple writes

numBytesReadFromFile:輸出時,從音頻文件中讀取的音頻數據的字節數。

到目前爲止好。 Apple在上面的示例代碼中聲明瞭numBytesReadFromFile,但對於我來說,這行代碼崩潰了!我得到一個EXC BAD ACCESS。

UInt32 numBytesReadFromFile; 

我需要聲明numBytesReadFromFile這樣,一切工作正常:

UInt32 numBytesReadFromFile = 2048; // 2048 = size of my buffer 

但是這個崩潰也

UInt32 numBytesReadFromFile = 12 
UInt32 numBytesReadFromFile = sizeof(UInt32) 

但這並不

UInt32 numBytesReadFromFile = 1021; // a random number 

我不是很有經驗enced C程序員,但據我所知,我通過聲明numBytesReadFromFile來保留一些內存,並且audiofilereadpacketdata方法將其數據寫入變量的地址。如果我錯了,請糾正我。

那麼它爲什麼會崩潰?我想我沒有解決真正的問題。

我的假設是我有某種多線程問題。當我準備隊列時,我在主線程中調用AudioFileReadPacketData並聲明

UInt32 numBytesReadFromFile; 

工作正常。我開始播放音頻,並調用一個回調函數,它在音頻隊列的內部後臺線程上調用AudioFilereadPacketData,併發生上述錯誤。如果我的假設是正確的,有人能夠更詳細地向我解釋這個問題,因爲我沒有多線程經驗。

謝謝。

+0

請發佈更多代碼。最初是什麼'ioNumBytes'?它是否符合您的緩衝區大小? – sbooth 2015-02-11 13:41:01

+0

沒有更多的代碼。一切都設置爲AudioFileReadDataPack。只需要聲明numBytesReadFromFile,並在調用AudioFileReadDataPack之前完成。 – Duc 2015-02-11 21:08:10

回答

2

參數ioNumBytesAudioFileReadPacketData是輸入/輸出參數。 documentation說:

輸入時,outBuffer參數的大小,以字節爲單位。輸出時,實際讀取的字節數爲 。

您將看到輸入和輸出值的區別,如果字節 尺寸爲您在ioNumPackets請求報文的數量 參數比你在outBuffer 參數傳遞的緩衝區大小小。在這種情況下,此參數的輸出值爲 ,小於其輸入值。

調用該函數時的值決定將多少數據寫入緩衝區。如果您發佈的代碼是正確的,numBytesReadFromFile永遠不會初始化爲_audioQueueBuffer->mAudioData的大小,並且程序崩潰,因爲它試圖將未確定數量的數據寫入_audioQueueBuffer->mAudioData。嘗試在函數調用之前設置參數:

UInt32 numBytesReadFromFile = _audioQueueBuffer->mAudioDataByteSize; 
OSStatus err = AudioFileReadPacketData(
            audioFile, // The audio file whose audio packets you want to read. 
            NO, // is cache set? 
            &numBytesReadFromFile, // On output, the number of bytes of audio data that were read from the audio file. 
            (AudioStreamPacketDescription *)_packetDescriptions, // array of packet descriptions of data that was read from the audio file (for CBR null) 
            currentPacket, // the next packet to be read into buffer 
            &numPackets, // number of actually read buffers 
            _audioQueueBuffer->mAudioData 
            ); 
+0

謝謝,但問題是Apple不會初始化numBytesReadFromFile。這裏是指南:[鏈接](https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/AQPlayback/PlayingAudio.html#//apple_ref/doc/uid/TP40005343-CH3-SW2 )清單3-4。那麼你能否澄清蘋果在輸入和輸出方面的含義?這是否意味着:首先它讀取存儲在numBytesreadFromFile中的值,然後將值存儲在numBytesReadFromFile中? – Duc 2015-02-11 21:16:28

+1

您鏈接的示例使用'AudioFileReadPackets',它是一個具有不同語義的舊API。使用'AudioFileReadPacketData',有必要在函數調用之前將'ioNumBytes'設置爲緩衝區可容納的字節數,並且如果函數沒有遇到錯誤,它將在返回時用該字節數替換該值實際閱讀。 – sbooth 2015-02-12 02:19:25

+0

謝謝!我沒有注意到有什麼區別。 – Duc 2015-02-12 12:14:20

相關問題