从麦克风捕捉时,耳机中的音量降至30%

我正在编写一个应用程序,通过Microsoft Media Foundation从麦克风捕捉声音。 这里是代码

#include <stdio.h> #include <assert.h> #include <mfapi.h> #include <mfidl.h> #include <Mfreadwrite.h> #include <Mferror.h> template <class T> void SafeRelease(T **ppT) { if (*ppT) { (*ppT)->Release(); *ppT = NULL; } } HRESULT ProcessSamples(IMFSourceReader *pReader, IMFSinkWriter *writer) { HRESULT hr = S_OK; IMFSample *pSample = NULL; size_t cSamples = 0; bool quit = false; while (!quit) { DWORD streamIndex, flags; LONGLONG llTimeStamp; hr = pReader->ReadSample( MF_SOURCE_READER_ANY_STREAM, // Stream index. 0, // Flags. &streamIndex, // Receives the actual stream index. &flags, // Receives status flags. &llTimeStamp, // Receives the time stamp. &pSample // Receives the sample or NULL. ); if (FAILED(hr)) { break; } wprintf(L"Stream %d (%I64d)\n", streamIndex, llTimeStamp); if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { wprintf(L"\tEnd of stream\n"); quit = true; } if (flags & MF_SOURCE_READERF_NEWSTREAM) { wprintf(L"\tNew stream\n"); } if (flags & MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED) { wprintf(L"\tNative type changed\n"); } if (flags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED) { wprintf(L"\tCurrent type changed\n"); } if (flags & MF_SOURCE_READERF_STREAMTICK) { wprintf(L"\tStream tick\n"); } if (pSample) { writer->WriteSample(streamIndex, pSample); ++cSamples; if (cSamples > 300)quit = true; } SafeRelease(&pSample); } if (FAILED(hr)) { wprintf(L"ProcessSamples FAILED, hr = 0x%x\n", hr); } else { wprintf(L"Processed %d samples\n", cSamples); } SafeRelease(&pSample); return hr; } int _tmain(int argc, _TCHAR* argv[]) { IMFMediaSource *source = 0; IMFMediaSession* m_pSession; IMFAttributes *pAttributes = NULL; HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { hr = MFStartup(MF_VERSION); } hr = MFCreateAttributes(&pAttributes, 2); if (SUCCEEDED(hr)) { hr = pAttributes->SetGUID( MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID ); } if (SUCCEEDED(hr)) { hr = MFCreateDeviceSource(pAttributes, &source); } SafeRelease(&pAttributes); hr = MFCreateAttributes(&pAttributes, 1); IMFSourceReader *pReader; hr = MFCreateSourceReaderFromMediaSource(source, NULL, &pReader); IMFMediaType *type; IMFSinkWriter *writer = 0; hr = MFCreateSinkWriterFromURL(L"D:\\wma.wma", NULL, NULL, &writer); IMFMediaType *inputType = 0; hr = MFCreateMediaType(&inputType); hr = inputType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio); hr = inputType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float); hr = inputType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2); hr = inputType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100); hr = inputType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, 8); hr =inputType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, 352800); hr = inputType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 32); hr = inputType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, 1); hr = inputType->SetUINT32(MF_MT_AUDIO_CHANNEL_MASK, 3); writer->SetInputMediaType(0, inputType, NULL); DWORD streamNum = 0; hr = writer->AddStream(inputType, &streamNum); hr = writer->BeginWriting(); ProcessSamples(pReader,writer); writer->Finalize(); return 0; } 

它可以很好地捕捉来自麦克风的声音,但是在调用pReader->ReadSample之后有不需要的function – 我的耳机中的音量降低。 我想这是媒体基础的特点,以获得最佳的捕捉质量,但我不需要它。 有没有办法来禁用这种行为? 提前致谢!

首先,欢迎来到StackOverflow!

您所描述的行为称为自动回避或流衰减 (Win7的新功能)。 它旨在用于通信应用程序。 由于您的耳机是通讯设备,因此会受到此行为的影响。 最简单的说,其他设备的音量在传入“通话”期间会减少。

关闭此功能的最简单方法是导航到“声音”控制面板上的最后一个选项卡(如下所示),您可以在其中更改音量选项。

在这里输入图像说明

否则,您可以提供此处显示的自定义回避行为。

您也可以使用类似于此处显示的代码来禁用自动回避功能,但请记住,您将混合使用WASAPI和Media Foundation,并且需要相应地管理会话。

希望这可以帮助。