Basiclly I just want to capture audio and save it to a file on disk. I came to the conlusion using the AVI format would be best and least annoying. After wading through the DirectShow API, this is what I came up with.
//I find the mic in previous code
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph);
//Initialize the Capture Graph Builder
hr = pBuild->SetFiltergraph(pGraph);
IBaseFilter *pMux;
//IFileSinkFilter *pSink;
hr = pBuild->SetOutputFileName(
&MEDIASUBTYPE_Avi, // Specifies AVI for the target file.
L"C:\\Example.avi", // File name.
&pMux, // Receives a pointer to the mux.
NULL); // (Optional) Receives a pointer to the file sink.
hr = pBuild->RenderStream(
&PIN_CATEGORY_CAPTURE, // Pin category.
&MEDIATYPE_Audio, // Media type.
pSrc, // Capture filter.
NULL, // Intermediate filter (optional).
pMux); // Mux or file sink filter.
Now, I figured this would be enough to do what I wanted but no file is created on disk and RenderStream gives me E_INVALDARGS. If I change the function call to:
hr = pBuild->RenderStream(
NULL, // Pin category.
NULL, // Media type.
pSrc, // Capture filter.
NULL, // Intermediate filter (optional).
pMux); // Mux or file sink filter.
I get the VFW_E_NOT_IN_GRAPH error instead.
What am I doing wrong here? I’d be outmost grateful for any help, thanks in advance!
VFW_E_NOT_IN_GRAPHmeans that you need to add yourpSrcfilter you created to the filter graph usingIGraphBuilder::AddFiltercall.To save audio-only stream into file, you might want to prefer .WAV format instead, Windows SDK comes with
WavDestproject/sample which can accept audio from audio filter and output a .WAV file stream compatible withFile Writer Filterto save into file.