2011-08-09 121 views
1

我正在嘗試製作一個使用ffmpeg,libmms播放音頻流的應用程序。
我可以打開MMS服務器,獲取流,並使用合適的編解碼器將音頻幀解碼爲原始幀。
但是我不知道下一步該怎麼做。
我想我必須使用AudioToolbox/AudioToolbox.h並製作audioqueue。
但是當我給audioqueuebuffer解碼緩衝區的內存和播放時,只會播放白噪聲。
這是我的代碼。Iphone流媒體和播放音頻問題

我在想什麼?
任何評論和提示是非常讚賞。
非常感謝。

while(av_read_frame(pFormatCtx, &pkt)>=0) 
{ 
    int pkt_decoded_len = 0; 
    int frame_decoded_len; 
    int decode_buff_remain=AVCODEC_MAX_AUDIO_FRAME_SIZE * 5; 
    if(pkt.stream_index==audiostream) 
    { 
     frame_decoded_len=decode_buff_remain; 
     int16_t *decode_buff_ptr = decode_buffer; 
     int decoded_tot_len=0; 
     pkt_decoded_len = avcodec_decode_audio2(pCodecCtx, decode_buff_ptr, &frame_decoded_len, 
               pkt.data, pkt.size); 
     if (pkt_decoded_len <0) break; 
     AudioQueueAllocateBuffer(audioQueue, kBufferSize, &buffers[i]); 
     AQOutputCallback(self, audioQueue, buffers[i], pkt_decoded_len); 

     if(i == 1){ 
      AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, 1.0); 
      AudioQueueStart(audioQueue, NULL); 
     } 
     i++; 
    } 
} 


void AQOutputCallback(void *inData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, int copySize) 
{ 
    mmsDemoViewController *staticApp = (mmsDemoViewController *)inData; 
    [staticApp handleBufferCompleteForQueue:inAQ buffer:inBuffer size:copySize]; 
} 

- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ 
          buffer:(AudioQueueBufferRef)inBuffer 
          size:(int)copySize 
{ 
    inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity; 
    memcpy((char*)inBuffer->mAudioData, (const char*)decode_buffer, copySize); 

    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL); 
} 

回答

1

您錯誤地調用了AQOutputCallback。您不必須調用該方法。
當音頻緩衝區使用音頻緩衝區時,將自動調用該方法。 而AQOutputCallback的原型是錯誤的。
根據你的代碼那個方法不會被自動調用,我想。
您可以覆蓋

typedef void (*AudioQueueOutputCallback) (
    void     *inUserData, 
    AudioQueueRef  inAQ, 
    AudioQueueBufferRef inBuffer 
); 

這樣

void AudioQueueCallback(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef 
         inBuffer); 

而且你應該設置音頻會話當你的應用程序啓動。
重要參考文獻是here.

但是,什麼是你願意解碼的音頻的擴展?
AudioStreamPacketDescription對於每個數據包的音頻是可變幀是很重要的。
否則,如果每一個數據包一幀,AudioStreamPacketDescription不顯着。

接下來你要做的是
要設置音頻會話,要使用解碼器獲取原始音頻幀,將幀放入音頻緩衝區。
而不是你,讓系統填滿空的緩衝區。

+0

謝謝我使用正確的原型後得到了一個解決方案 – KayKay