使用 MediaFoundation的IMFSourceReader读取音频(视频)文件的音轨,转换成PCM格式用DSound播放。
主要步骤:
1. 初始化
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hr = MFStartup(MF_VERSION);
2. 打开媒体文件
MFCreateSourceReaderFromURL(wszFileName, NULL, &m_pReader)
3. 创建转码后的媒体格式(PCM)
MFCreateMediaType(&pPartialType);
pPartialType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
pPartialType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
4. 选择音轨
m_pReader->SetStreamSelection((DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, TRUE);
m_pReader->SetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, NULL, pPartialType);
5. 从当前音轨取得PCM的参数到 WAVFORMATEX 结构
m_pReader->GetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, &pUncompressedAudioType);
MFCreateWaveFormatExFromMFMediaType(pUncompressedAudioType, &m_pWav, &cbFormat);
6. 初始化 DirectSound
具体参考:雷霄骅(leixiaohua1020)的专栏 http://blog.csdn.net/leixiaohua1020/article/details/40540147
7. 循环读取音频数据并提交给 DirectSound 的输入缓冲区
使用的API 是 :m_pReader->ReadSample, pSample->ConvertToContiguousBuffer,pBuffer->Lock,pBuffer->Unlock
播放已经分装到CAudioPlayer类。
源代码已上传至CSDN: http://download.csdn.net/detail/lsong98cn/9595625