Windows Media Foundation recording audio

本秂侑毒 提交于 2019-11-27 15:42:34

I apologize for the late response, and I hope you can still find this valuable. I recently completed a project similar to yours (recording webcam video along with a selected microphone to a single video file with audio). The key is to creating an aggregate media source.

// http://msdn.microsoft.com/en-us/library/windows/desktop/dd388085(v=vs.85).aspx
HRESULT CreateAggregateMediaSource(IMFMediaSource *videoSource, IMFMediaSource *audioSource, IMFMediaSource **aggregateSource)
{
    *aggregateSource = NULL;
    IMFCollection *pCollection = NULL;

    HRESULT hr = MFCreateCollection(&pCollection);

    if (SUCCEEDED(hr))
        hr = pCollection->AddElement(videoSource);

    if (SUCCEEDED(hr))
        hr = pCollection->AddElement(audioSource);

    if (SUCCEEDED(hr))
        hr = MFCreateAggregateSource(pCollection, aggregateSource);

    SafeRelease(&pCollection);
    return hr;
}

When configuring the sink writer, you will add 2 streams (one for audio and one for video). Of course, you will also configure the writer correctly for the input stream types.

HRESULT        hr = S_OK;
IMFMediaType  *videoInputType = NULL;
IMFMediaType  *videoOutputType = NULL;
DWORD          videoOutStreamIndex = 0;
DWORD          audioOutStreamIndex = 0;
IMFSinkWriter *writer = NULL;

// [other create and configure writer]

if (SUCCEEDED(hr))
    hr = writer->AddStream(videoOutputType, &videoOutStreamIndex);    

// [more configuration code]

if (SUCCEEDED(hr))
    hr = writer->AddStream(audioOutputType, &audioOutStreamIndex);

Then when reading the samples, you will need to pay close attention to the reader streamIndex, and sending them to the writer appropriately. You will also need to pay close attention to the format that the codec expects. For instance, IEEE float vs PCM, etc. Good luck, and I hope it is not to late.

Roman R.

Did you have hard time to manage DirectShow audio capture in Record directshow audio device to file?

Capturing with Media Foundation is hardly any simpler. Not even mentioning that in general there are a lot more resources on DirectShow out there....

MSDN offers you a WavSink Sample that implements audio capture into file:

Shows how to implement a custom media sink in Microsoft Media Foundation. The sample implements an archive sink that writes uncompressed PCM audio to a .wav file.

I am not sure why they decided to not make this a standard component. Having Media Foundation inferior to DirectShow in many ways, they could at least make this small thing as an advantage. Anyway, you have the sample and it looks like a good start.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!