Windows Media Foundation recording audio

后端 未结 2 764
执念已碎
执念已碎 2020-12-03 23:20

I\'m using the windows media foundation api to enumerate both my microphones and available cameras, which both work.

Here is my enumeration code:

cla         


        
相关标签:
2条回答
  • 2020-12-03 23:45

    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.

    0 讨论(0)
  • 2020-12-03 23:59

    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 = nullptr;
        IMFCollection *pCollection = nullptr;
    
        HRESULT hr = ::MFCreateCollection(&pCollection);
    
        if (S_OK == hr)
            hr = pCollection->AddElement(videoSource);
    
        if (S_OK == hr)
            hr = pCollection->AddElement(audioSource);
    
        if (S_OK == 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      = nullptr;
    IMFMediaType  *videoOutputType     = nullptr;
    DWORD          videoOutStreamIndex = 0u;
    DWORD          audioOutStreamIndex = 0u;
    IMFSinkWriter *writer              = nullptr;
    
    // [other create and configure writer]
    
    if (S_OK == hr))
        hr = writer->AddStream(videoOutputType, &videoOutStreamIndex);    
    
    // [more configuration code]
    
    if (S_OK == 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 too late.

    0 讨论(0)
提交回复
热议问题