BOOL CMixer::SetMute(DWORD dwSrcType, BOOL bValue) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_MUTE)) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_BOOLEAN mxcd_f; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cChannels = 1; mxcd.cMultipleItems = 0; mxcd.cbDetails = sizeof(mxcd_f); mxcd.paDetails = &mxcd_f; mxcd_f.fValue = bValue; if (! SetControlDetails(&mxcd, MIXER_OBJECTF_MIXER)) return FALSE; return TRUE; } BOOL CMixer::GetMute(DWORD dwSrcType, BOOL* pbValue) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT )) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_BOOLEAN mxcd_f; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cChannels = 1; mxcd.cMultipleItems = 0; mxcd.cbDetails = sizeof(mxcd_f); mxcd.paDetails = &mxcd_f; if (! GetControlDetails(&mxcd, MIXER_GETCONTROLDETAILSF_VALUE)) return FALSE; *pbValue = mxcd_f.fValue; return TRUE; } |
BOOL bValue = TRUE; SetMute(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, bValue); GetMute(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &bValue); |
afx_msg void OnMLChange(WPARAM wParam, LPARAM lParam); afx_msg void OnMCChange(WPARAM wParam, LPARAM lParam); ON_MESSAGE(MM_MIXM_LINE_CHANGE, OnMLChange) ON_MESSAGE(MM_MIXM_CONTROL_CHANGE, OnMCChange) |
wParam = (WPARAM) hMixer lParam = (LPARAM) dwControlID |
wParam = (WPARAM) hMixer lParam = (LPARAM) dwLineID |
void CMixerControlDlg::OnMCChange(WPARAM wParam, LPARAM lParam) { DWORD dwLValue; DWORD dwRValue; GetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &dwLValue,&dwRValue); //GetVolume函数的定义见下面,然后根据返回的值调整滑动条的位置 m_SliderVolR.SetPos(MAX_VOL_VALUE - dwLValue); m_SliderVolL.SetPos(MAX_VOL_VALUE - dwRValue); //你也可以在这里调用GetMute查看Volume是否被静音, } |
如此你的程序就可以自动的响应系统设置的改变了。
关于mixer API的应用开发就介绍到这里,记着最后要关闭mixer如此:mixerClose(m_hmx);
关于工程的建立我就不多少了,很简单的,就是一个基于对话框的工程,上面放了一些控件。下面我主要讲一下每个功能是如何实现的。主要有三个功能1 如何调整左右声道音量的大小,2 如何将某个设备静音,3 如何选择录音设备。
这里关于mixer函数的用法还要先唠叨几句。一般来说,对音频线路的操作流程如下:
1、通过GetLineInfo获取指定音频线路的信息,返回一个MIXERLINE结构
2、然后通过GetLineControl获取音频线路相关的控制的通用信息,通过MIXERCONTROL结构返回。
3、通过GetConrolDetails获取指定控制的属性值
4、通过SetControlDetails设置指定控制的属性值,
对于每个线路设备,mixer都用一个类型值来标示,比如:
Volume对应的值 MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
CD 对于的值 MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC
Midi对应的值为 MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER
Wave对应的值为 MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
Line in对应的值为 MIXERLINE_COMPONENTTYPE_SRC_LINE
Microphone对应的值为 MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
我们可以通过音频线路的类型值获得相应的线路的信息,也可以通过音频线路的设备ID来获取相应的线路的信息。
下面开始我们编程吧。
首先定义三个变量
UINT m_uMxId; //mixer的ID HWND m_hWnd; //回调窗口句柄 HMIXER m_hmx; // |
#define MAX_VOL_VALUE 65535 if (MMSYSERR_NOERROR != mixerOpen(&m_hmx, m_uMxId,(DWORD)m_hWnd, 0, CALLBACK_WINDOW)) { return FALSE; } if (MMSYSERR_NOERROR == mixerGetID((HMIXEROBJ)m_hmx, &m_uMxId, MIXER_OBJECTF_HMIXER)) { return m_uMxId; } //设置Volume的滑动条的范围这里只以Volume为例。 m_SliderWaveL.SetRange(0, MAX_VOL_VALUE, TRUE); m_SliderWaveR.SetRange(0, MAX_VOL_VALUE, TRUE); |
DWORD dwLValue; DWORD dwRValue; GetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &dwLValue,&dwRValue); //GetVolume函数的定义见下面,然后根据返回的值调整滑动条的位置 m_SliderVolR.SetPos(MAX_VOL_VALUE - dwLValue); m_SliderVolL.SetPos(MAX_VOL_VALUE - dwRValue); |
void CMixerControlDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // m_dwSpkR和m_dwSpkL是用来记录Volume左右声道的音量值,0~~65535 CSliderCtrl *pSlider = (CSliderCtrl *)pScrollBar; int nValue = MAX_VOL_VALUE - pSlider->GetPos(); //获取滑动条的位置pos else if (m_SliderVolR.m_hWnd == pSlider->m_hWnd) { //如果拖动的是Volume的左声道 m_dwSpkR = nValue; // 设置Volume的音量值 SetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, m_dwSpkL, m_dwSpkR); } else if (m_SliderVolL.m_hWnd == pSlider->m_hWnd) { //Volume右声道 m_dwSpkL = nValue; // 设置Volume的音量值 SetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, m_dwSpkL, m_dwSpkR); } //其他音频线路可以依次类推在下面添加 } |
BOOL CMixer::SetVolume(DWORD dwSrcType, DWORD dwLValue, DWORD dwRValue, BOOL bMono) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_VOLUME)) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_UNSIGNED mxcd_u1; MIXERCONTROLDETAILS_UNSIGNED mxcd_u[2]; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cMultipleItems = 0; if (bMono) { mxcd.cChannels = 1; mxcd.cbDetails = sizeof(mxcd_u1); mxcd.paDetails = &mxcd_u1; mxcd_u1.dwValue = dwLValue; } else { mxcd.cChannels = mxl.cChannels; mxcd.cbDetails = sizeof(*mxcd_u); mxcd.paDetails = mxcd_u; mxcd_u[0].dwValue = dwLValue; mxcd_u[1].dwValue = dwRValue; } if (! SetControlDetails(&mxcd, MIXER_OBJECTF_MIXER)) return FALSE; return TRUE; } BOOL CMixer::GetVolume(DWORD dwSrcType, DWORD* pdwLValue, DWORD* pdwRValue, BOOL bMono) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_VOLUME)) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_UNSIGNED mxcd_u1; MIXERCONTROLDETAILS_UNSIGNED mxcd_u[2]; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cMultipleItems = 0; if (bMono) { mxcd.cChannels = 1; mxcd.cbDetails = sizeof(mxcd_u1); mxcd.paDetails = &mxcd_u1; if (! GetControlDetails(&mxcd, MIXER_GETCONTROLDETAILSF_VALUE)) return FALSE; *pdwLValue = mxcd_u1.dwValue; } else { mxcd.cChannels = mxl.cChannels; mxcd.cbDetails = sizeof(*mxcd_u); mxcd.paDetails = mxcd_u; if (! GetControlDetails(&mxcd, MIXER_GETCONTROLDETAILSF_VALUE)) return FALSE; *pdwLValue = mxcd_u[0].dwValue; *pdwRValue = mxcd_u[1].dwValue; } return TRUE; } BOOL GetLineInfo(LPMIXERLINE pmxl, DWORD dwDstType, DWORD dwSrcType) { MIXERCAPS mxcaps; if (! GetDevCaps(&mxcaps)) return FALSE; UINT u=0; do { pmxl->cbStruct = sizeof(*pmxl); pmxl->dwDestination = u; u++; if (MMSYSERR_NOERROR != mixerGetLineControls((HMIXEROBJ)m_uMxId, pmxl, MIXER_GETLINEINFOF_DESTINATION)) { return FALSE; } } while ((u < mxcaps.cDestinations) && (pmxl->dwComponentType != dwDstType)); if (u > mxcaps.cDestinations) return FALSE; if (dwDstType == dwSrcType) return TRUE; pmxl->dwDestination = u; UINT cConnections = (UINT)pmxl->cConnections; UINT v=0; u--; do { pmxl->cbStruct = sizeof(*pmxl); pmxl->dwDestination = u; pmxl->dwSource = v; v++; if (MMSYSERR_NOERROR != mixerGetLineControls((HMIXEROBJ)m_uMxId, pmxl, MIXER_GETLINEINFOF_SOURCE)) { return FALSE; } } while ((v < cConnections) && (pmxl->dwComponentType != dwSrcType)); if((v > cConnections) || (pmxl->dwComponentType !=dwSrcType)) return FALSE; return TRUE; } BOOL GetLineControl(LPMIXERCONTROL pmxc, LPMIXERLINE pmxl, DWORD dwType) { LPMIXERCONTROL pamxctrl; DWORD cbmxctrls = sizeof(*pamxctrl) * (UINT)pmxl->cControls; pamxctrl = (LPMIXERCONTROL)LocalAlloc(LPTR, cbmxctrls); MIXERLINECONTROLS mxlc; mxlc.cbStruct = sizeof(mxlc); mxlc.dwLineID = pmxl->dwLineID; mxlc.dwControlType = dwType; mxlc.cControls = pmxl->cControls; mxlc.cbmxctrl = sizeof(*pamxctrl); mxlc.pamxctrl = pamxctrl; if (MMSYSERR_NOERROR != mixerGetControlDetails((HMIXEROBJ)m_uMxId, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE)) { return FALSE; } memcpy(pmxc, pamxctrl, sizeof(*pamxctrl)); LocalFree(pamxctrl); return TRUE; } |
BOOL CMixer::SetMute(DWORD dwSrcType, BOOL bValue) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_MUTE)) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_BOOLEAN mxcd_f; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cChannels = 1; mxcd.cMultipleItems = 0; mxcd.cbDetails = sizeof(mxcd_f); mxcd.paDetails = &mxcd_f; mxcd_f.fValue = bValue; if (! SetControlDetails(&mxcd, MIXER_OBJECTF_MIXER)) return FALSE; return TRUE; } BOOL CMixer::GetMute(DWORD dwSrcType, BOOL* pbValue) { MIXERLINE mxl; if (! GetLineInfo(&mxl, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, dwSrcType)) return FALSE; MIXERCONTROL mxc; if (! GetLineControl(&mxc, &mxl, MIXERCONTROL_CONTROLTYPE_MULTIPLESELECT )) return FALSE; MIXERCONTROLDETAILS mxcd; MIXERCONTROLDETAILS_BOOLEAN mxcd_f; mxcd.cbStruct = sizeof(mxcd); mxcd.dwControlID = mxc.dwControlID; mxcd.cChannels = 1; mxcd.cMultipleItems = 0; mxcd.cbDetails = sizeof(mxcd_f); mxcd.paDetails = &mxcd_f; if (! GetControlDetails(&mxcd, MIXER_GETCONTROLDETAILSF_VALUE)) return FALSE; *pbValue = mxcd_f.fValue; return TRUE; } |
BOOL bValue = TRUE; SetMute(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, bValue); GetMute(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &bValue); |
afx_msg void OnMLChange(WPARAM wParam, LPARAM lParam); afx_msg void OnMCChange(WPARAM wParam, LPARAM lParam); ON_MESSAGE(MM_MIXM_LINE_CHANGE, OnMLChange) ON_MESSAGE(MM_MIXM_CONTROL_CHANGE, OnMCChange) |
wParam = (WPARAM) hMixer lParam = (LPARAM) dwControlID |
wParam = (WPARAM) hMixer lParam = (LPARAM) dwLineID |
void CMixerControlDlg::OnMCChange(WPARAM wParam, LPARAM lParam) { DWORD dwLValue; DWORD dwRValue; GetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &dwLValue,&dwRValue); //GetVolume函数的定义见下面,然后根据返回的值调整滑动条的位置 m_SliderVolR.SetPos(MAX_VOL_VALUE - dwLValue); m_SliderVolL.SetPos(MAX_VOL_VALUE - dwRValue); //你也可以在这里调用GetMute查看Volume是否被静音, } |