SDL+FFMPEG 音頻解碼和播放流程

來源:互聯網
上載者:User

1.設定音頻參數和相應的回呼函數,當聲音裝置需要資料時SDL會自動調用該回呼函數,填充該結構體。

typedef struct SDL_AudioSpec {int freq;/**< DSP frequency -- samples per second */Uint16 format;/**< Audio data format */Uint8  channels;/**< Number of channels: 1 mono, 2 stereo */Uint8  silence;/**< Audio buffer silence value (calculated) */Uint16 samples;/**< Audio buffer size in samples (power of 2) */Uint16 padding;/**< Necessary for some compile environments */Uint32 size;/**< Audio buffer size in bytes (calculated) *//** *  This function is called when the audio device needs more data. * *  @param[out] streamA pointer to the audio data buffer *  @param[in]  lenThe length of the audio buffer in bytes. * *  Once the callback returns, the buffer will no longer be valid. *  Stereo samples are stored in a LRLRLR ordering. */void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);void  *userdata;} SDL_AudioSpec;

2.開啟聲音裝置

SDL_OpenAudio(&wanted_spec, &spec)

3.開始播放

SDL_PauseAudio(0);

4.在回呼函數中解碼音頻流,將解碼後的buffer複製到stream中

void audio_callback(void *userdata, Uint8 *stream, int len) {  //從檔案中讀取資料包,解碼到audio_buf中  .....  memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);  .....}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.