网上此类贴子很多,工作原理就不用介绍了,请看代码:
- // AudioInputChannel.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <tchar.h>
- #include <windows.h>
- #ifndef _INC_MMSYSTEM
- #include <mmsystem.h>
- #pragma comment(lib, "winmm.lib")
- #endif
- void PrintMixerLinesInfo(void);
- bool ActiveMixerInputChannel(UINT, UINT);
- int _tmain(int argc, _TCHAR* argv[])
- {
- UINT di, li;
- PrintMixerLinesInfo ();
- _tcprintf( TEXT("输入设备编号与录音通道编号(例:0 1)") );
- _tscanf ( TEXT("%d %d"), &di, &li );
- if ( !ActiveMixerInputChannel ( di, li ) ){
- _tcprintf( TEXT("设置失败!/n") );
- } else {
- _tcprintf( TEXT("设置成功!/n") );
- PrintMixerLinesInfo ();
- }
- system("pause");
- return 0;
- }
- void PrintMixerLinesInfo(void)
- {
- MIXERCAPS mxcaps = {0};
- HMIXER hMixer = NULL;
- for ( UINT i = 0; i < mixerGetNumDevs (); i++ ){
- if ( MMSYSERR_NOERROR != mixerGetDevCaps ( i, &mxcaps, sizeof ( MIXERCAPS ) ) ){ continue; }
- if ( MMSYSERR_NOERROR != mixerOpen ( &hMixer, i, NULL, NULL, 0 ) ){ continue; }
- MIXERLINE mxl = { sizeof ( MIXERLINE ), 0, 0, 0, 0, 0, MIXERLINE_COMPONENTTYPE_DST_WAVEIN, 0};
- if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE ) ){
- mixerClose ( hMixer );
- continue;
- }
- _tcprintf( TEXT("%d: %s /t[%s]/n"), i, mxcaps.szPname, mxl.szName );
- MIXERCONTROL mxc = { sizeof ( MIXERCONTROL ), 0 };
- MIXERLINECONTROLS mxlc = { sizeof ( MIXERLINECONTROLS ), mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_MIXER, 1, mxc.cbStruct, &mxc };
- if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){
- mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
- if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){
- mixerClose ( hMixer );
- continue;
- }
- }
- if ( mxc.cMultipleItems == 0 ){
- mixerClose ( hMixer );
- continue;
- }
- MIXERCONTROLDETAILS mxcd = { sizeof ( MIXERCONTROLDETAILS ), mxc.dwControlID, 1, 0 };
- PMIXERCONTROLDETAILS_LISTTEXT mxcdlts = (PMIXERCONTROLDETAILS_LISTTEXT)malloc ( sizeof ( MIXERCONTROLDETAILS_LISTTEXT ) * mxc.cMultipleItems );
- mxcd.cbDetails = sizeof ( MIXERCONTROLDETAILS_LISTTEXT );
- mxcd.cMultipleItems = mxc.cMultipleItems;
- mxcd.paDetails = mxcdlts;
- if ( MMSYSERR_NOERROR != mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_LISTTEXT ) ){
- free ( mxcdlts );
- mixerClose ( hMixer );
- continue;
- }
- PMIXERCONTROLDETAILS_BOOLEAN mxcdbls = (PMIXERCONTROLDETAILS_BOOLEAN)malloc ( sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
- mxcd.cbDetails = sizeof ( MIXERCONTROLDETAILS_BOOLEAN );
- mxcd.cMultipleItems = mxc.cMultipleItems;
- mxcd.paDetails = mxcdbls;
- if ( MMSYSERR_NOERROR != mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_VALUE ) ){
- free ( mxcdbls );
- mixerClose ( hMixer );
- continue;
- }
- DWORD dwConnections = mxl.cConnections;
- DWORD dwDestination = mxl.dwDestination;
- for ( DWORD j = 0; j < dwConnections; j++ ){
- mxl.dwDestination = dwDestination;
- mxl.dwSource = j;
- if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_DESTINATION | MIXER_GETLINEINFOF_SOURCE ) ){ continue; }
- for ( DWORD k = 0; k < mxc.cMultipleItems; k++ ){
- if ( mxcdlts[k].dwParam1 == mxl.dwLineID ){
- _tcprintf( TEXT("/t%d %s /t%s/n"), j, mxl.szName, mxcdbls[k].fValue ? TEXT("Enabled") : TEXT("Disabled") );
- break;
- }
- }
- }
- _tcprintf( TEXT("/n") );
- free ( mxcdbls );
- free ( mxcdlts );
- mixerClose ( hMixer );
- }
- }
- bool ActiveMixerInputChannel(UINT nMixerDeviceIndex, UINT nMixerLineIndex)
- {
- bool result = false;
- HMIXER hMixer = NULL;
- MIXERCONTROL mxc = { sizeof ( MIXERCONTROL ), 0 };
- MIXERLINE mxl = { sizeof ( MIXERLINE ), 0, 0, 0, 0, 0, MIXERLINE_COMPONENTTYPE_DST_WAVEIN, 0};
- if ( MMSYSERR_NOERROR != mixerOpen ( &hMixer, nMixerDeviceIndex, NULL, NULL, 0 ) ){ return false; }
- if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE ) ){
- mixerClose ( hMixer );
- return false;
- }
- MIXERLINECONTROLS mxlc = { sizeof ( MIXERLINECONTROLS ), mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_MIXER, 1, mxc.cbStruct, &mxc };
- mxl.dwSource = nMixerLineIndex;
- if ( MMSYSERR_NOERROR != mixerGetLineInfo ( (HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_DESTINATION | MIXER_GETLINEINFOF_SOURCE ) ){
- mixerClose ( hMixer );
- return false;
- }
- if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){
- mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUX;
- if ( MMSYSERR_NOERROR != mixerGetLineControls ( (HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE ) ){
- mixerClose ( hMixer );
- return false;
- }
- }
- if ( mxc.cMultipleItems == 0 ){ mixerClose ( hMixer ); return false; }
- PMIXERCONTROLDETAILS_BOOLEAN mxcdbls = (PMIXERCONTROLDETAILS_BOOLEAN)malloc ( sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
- PMIXERCONTROLDETAILS_LISTTEXT mxcdlts = (PMIXERCONTROLDETAILS_LISTTEXT)malloc ( sizeof ( MIXERCONTROLDETAILS_LISTTEXT ) * mxc.cMultipleItems );
- MIXERCONTROLDETAILS mxcd = { sizeof ( MIXERCONTROLDETAILS ), mxc.dwControlID, 1, (HWND)mxc.cMultipleItems, sizeof ( MIXERCONTROLDETAILS_LISTTEXT ), mxcdlts };
- if ( MMSYSERR_NOERROR == mixerGetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_GETCONTROLDETAILSF_LISTTEXT ) ){
- memset ( mxcdbls, 0, sizeof ( MIXERCONTROLDETAILS_BOOLEAN ) * mxc.cMultipleItems );
- for ( DWORD i = 0; i < mxc.cMultipleItems; i++ ){
- if ( mxcdlts[i].dwParam1 == mxl.dwLineID ){
- mxcdbls[i].fValue = 1;
- mxcd.paDetails = mxcdbls;
- mxcd.cbDetails = sizeof ( MIXERCONTROLDETAILS_BOOLEAN );
- result = (MMSYSERR_NOERROR == mixerSetControlDetails ( (HMIXEROBJ)hMixer, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE ) ); break;
- }
- }
- }
- free ( mxcdbls );
- free ( mxcdlts );
- mixerClose ( hMixer );
- return result;
- }