使用 Longman Dictionary Helper的朋友, 你會發現我們必須每次到 [控制台]->[聲音及音訊裝置]-> 設定錄音來源. 才能正常錄音, 能不能自動設定呢?
不好意思, 因為我真的沒時間加程式碼進去. 其實使用 IAMAudioInputMixer 介面, 就可以達到自動化的工作.
下面是自動設定錄音音源的關鍵程式碼
(完整程式碼: 請下載 WindowsSDK, 位置: Microsoft SDKs\Windows\v6.0\Samples\Multimedia\DirectShow\Capture\AMCap)
// 設定 audio 輸入 pin 的屬性 (讀取目前的設定值)
HRESULT SetInputPinProperties(IAMAudioInputMixer *pPinMixer) {
HRESULT hr=0;
BOOL bLoudness=0, bMono=0;
double dblBass=0, dblBassRange=0, dblMixLevel=0,
dblPan=0, dblTreble=0, dblTrebleRange=0;
// Read current settings for debugging purposes. Many of these interfaces
// will not be implemented by the input device's driver, so they will
// return E_NOTIMPL. In that case, just ignore the values.
hr = pPinMixer->get_Bass(&dblBass);
hr = pPinMixer->get_BassRange(&dblBassRange);
hr = pPinMixer->get_Loudness(&bLoudness);
hr = pPinMixer->get_MixLevel(&dblMixLevel);
hr = pPinMixer->get_Mono(&bMono);
hr = pPinMixer->get_Pan(&dblPan);
hr = pPinMixer->get_Treble(&dblTreble);
hr = pPinMixer->get_TrebleRange(&dblTrebleRange);
// Manipulate desired values here (mono/stereo, pan, etc.)
// Since some of these methods may fail, just return success.
// This function is for demonstration purposes only.
return S_OK;
}
// 啟動選擇的 輸入裝置
HRESULT ActivateSelectedInputPin(IBaseFilter *m_pInputDevice,CList< DeviceData*> &m_ListInputPins,int nActivePin) {
HRESULT hr=S_OK;
IPin *pPin=0;
IAMAudioInputMixer *pPinMixer;
// Step 1:
// 從事先列舉出的 裝置 Moniker 串列中,
// 取得到底有哪些音源輸入 pin 接腳
int nPins = (int)m_ListInputPins.GetCount();
for (int i=0; i < nPins; i++){
// Step 2: 從你的音效裝置取得 IAMAudioInputMixer 介面
// 你可以用這個裝置設定 哪些音源輸入 pin 要 disable 或 致能
hr = GetPin(m_pInputDevice, PINDIR_INPUT, i, &pPin);
if (SUCCEEDED(hr)){
hr = pPin->QueryInterface(IID_IAMAudioInputMixer, (void **)&pPinMixer);
if (SUCCEEDED(hr)){
// ------------- 關鍵片段 -----------------
if (i == nActivePin){
// 你也可以利用 IAMAudioInputMixer 設定如音量等屬性
hr = SetInputPinProperties(pPinMixer);
// 若你的系統只有一個 input,
// 那麼設定 Enable 可能會傳回 E_NOTIMPL
hr = pPinMixer->put_Enable(TRUE);
}else {
hr = pPinMixer->put_Enable(FALSE);
}
pPinMixer->Release();
}
// Release pin interfaces
pPin->Release();
}
}// end of 所有音源 pin 處理
return hr;
}
參考資料:
1. Microsoft SDK \Samples\Multimedia\DirectShow\Capture\AudioCap
您好,看了您的代码非常有启发、谢谢!
回覆刪除我想请教一个问题:在windows-XP上目前我可以控制Line-In麦克风的音量、但是无法控制USB-麦克风的音量:请问在windows-XP上、可以用IAMAudioInputMixer来控制USB-麦克风的音量吗?谢谢!