2012-03-19 64 views
0

我想寫一個播放器來播放音樂。我看到類似下面的代碼:此隊列屬性(iOS音頻隊列)的含義是什麼?

AudioFileGetPropertyInfo(audioFile, 
         kAudioFilePropertyMagicCookieData, &size, nil); 

if (size > 0) { 
    cookie = malloc(sizeof(char) * size); 
    AudioFileGetProperty(audioFile, 
         kAudioFilePropertyMagicCookieData, &size, cookie); 
    AudioQueueSetProperty(aduioQueue, 
          kAudioQueueProperty_MagicCookie, cookie, size); 
    free(cookie); 
} 

我不知道爲什麼要設置AudioQueueProperty,什麼是關於kAudioQueueProperty_MagicCookie的手段?我無法從文檔中找到幫助。 誰可以給出解決問題的方向。

回答

1

「magic cookie」是一個文件類型簽名,由文件開頭的一個唯一字節序列組成,指示文件格式。音頻隊列框架使用此信息來確定如何解碼或從文件流中提取音頻信息(而不是使用或信任文件擴展名)。您發佈的代碼從文件中讀取這組字節,並將其作爲cookie傳遞給音頻隊列。 (例如,讓它們被解釋爲PCM樣本是錯誤的)。

+0

感謝您的幫助。我找到關於kAudioQueueProperty_MagicCookie的文檔。它說:「如果你正在播放或錄製的音頻格式需要一個魔術餅乾,你必須在排入任何緩衝區之前爲這個屬性設置一個值。」...在我的應用程序中,我清除了上面的代碼,聲音是好。我的問題是,音頻隊列有能力估計文件格式? – Henry 2012-03-20 04:54:16

1

其實魔術餅乾不僅僅是一個簽名,它還包含一些關於編碼器的信息,最有用的項目是「最大比特率」和「平均比特率」,專門用於像AudioFileMPEG4Type這樣的壓縮格式。對於這種特定類型的魔術餅乾與MPEG-4數據文件中的「esds」框相同。你可以找到確切位設置:

http://xhelmboyx.tripod.com/formats/mp4-layout.txt


8+字節VERS。 2 ES描述符框 =長UNSIGNED偏移+長ASCII文本串 '的esds' - 如果編碼以ISO/IEC 14496-10 AVC標準然後任選地使用: =長UNSIGNED偏移+長ASCII文本串 'm4ds'

   -> 4 bytes version/flags = 8-bit hex version + 24-bit hex flags 
        (current = 0) 

       -> 1 byte ES descriptor type tag = 8-bit hex value 0x03 
       -> 3 bytes extended descriptor type tag string = 3 * 8-bit hex value 
        - types are Start = 0x80 ; End = 0xFE 
        - NOTE: the extended start tags may be left out 
       -> 1 byte descriptor type length = 8-bit unsigned length 

        -> 2 bytes ES ID = 16-bit unsigned value 
        -> 1 byte stream priority = 8-bit unsigned value 
        - Defaults to 16 and ranges from 0 through to 31 

        -> 1 byte decoder config descriptor type tag = 8-bit hex value 0x04 
        -> 3 bytes extended descriptor type tag string = 3 * 8-bit hex value 
         - types are Start = 0x80 ; End = 0xFE 
         - NOTE: the extended start tags may be left out 
        -> 1 byte descriptor type length = 8-bit unsigned length 

         -> 1 byte object type ID = 8-bit unsigned value 
         - type IDs are system v1 = 1 ; system v2 = 2 
         - type IDs are MPEG-4 video = 32 ; MPEG-4 AVC SPS = 33 
         - type IDs are MPEG-4 AVC PPS = 34 ; MPEG-4 audio = 64 
         - type IDs are MPEG-2 simple video = 96 
         - type IDs are MPEG-2 main video = 97 
         - type IDs are MPEG-2 SNR video = 98 
         - type IDs are MPEG-2 spatial video = 99 
         - type IDs are MPEG-2 high video = 100 
         - type IDs are MPEG-2 4:2:2 video = 101 
         - type IDs are MPEG-4 ADTS main = 102 
         - type IDs are MPEG-4 ADTS Low Complexity = 103 
         - type IDs are MPEG-4 ADTS Scalable Sampling Rate = 104 
         - type IDs are MPEG-2 ADTS = 105 ; MPEG-1 video = 106 
         - type IDs are MPEG-1 ADTS = 107 ; JPEG video = 108 
         - type IDs are private audio = 192 ; private video = 208 
         - type IDs are 16-bit PCM LE audio = 224 ; vorbis audio = 225 
         - type IDs are dolby v3 (AC3) audio = 226 ; alaw audio = 227 
         - type IDs are mulaw audio = 228 ; G723 ADPCM audio = 229 
         - type IDs are 16-bit PCM Big Endian audio = 230 
         - type IDs are Y'CbCr 4:2:0 (YV12) video = 240 ; H264 video = 241 
         - type IDs are H263 video = 242 ; H261 video = 243 
         -> 6 bits stream type = 3/4 byte hex value 
         - type IDs are object descript. = 1 ; clock ref. = 2 
         - type IDs are scene descript. = 4 ; visual = 4 
         - type IDs are audio = 5 ; MPEG-7 = 6 ; IPMP = 7 
         - type IDs are OCI = 8 ; MPEG Java = 9 
         - type IDs are user private = 32 
         -> 1 bit upstream flag = 1/8 byte hex value 
         -> 1 bit reserved flag = 1/8 byte hex value set to 1 
         -> 3 bytes buffer size = 24-bit unsigned value 
         -> 4 bytes maximum bit rate = 32-bit unsigned value 
         -> 4 bytes average bit rate = 32-bit unsigned value 

         -> 1 byte decoder specific descriptor type tag 
          = 8-bit hex value 0x05 
         -> 3 bytes extended descriptor type tag string 
          = 3 * 8-bit hex value 
          - types are Start = 0x80 ; End = 0xFE 
          - NOTE: the extended start tags may be left out 
         -> 1 byte descriptor type length 
          = 8-bit unsigned length 

          -> ES header start codes = hex dump 

        -> 1 byte SL config descriptor type tag = 8-bit hex value 0x06 
        -> 3 bytes extended descriptor type tag string = 3 * 8-bit hex value 
         - types are Start = 0x80 ; End = 0xFE 
         - NOTE: the extended start tags may be left out 
        -> 1 byte descriptor type length = 8-bit unsigned length 

         -> 1 byte SL value = 8-bit hex value set to 0x02 

魔術曲奇即來自kAudioFilePropertyMagicCookieData從ES描述符開始(只是忽略在圖中描述的第一個4個字節和其餘的將是到magick餅乾精確匹配)。

樣品魔餅會是此:

03 80 80 80 22 00 00 00 04 80 80 80 14 40 15 00 18 00 00 00 FA 00 00 00 FA 00 05 80 80 80 02 12 08 06 80 80 80 01 02 

最大位速率是在偏移18 - >從0xFA00(或64000) 平均比特速率是在偏移22 - >從0xFA00(或64000)

雖然根據蘋果文檔,魔餅是可讀/寫的,但在創建或轉換文件之前,我沒有機會更改比特率。

希望能幫助別人。