如何同步更改系统音量值

JackLucifer 2003-12-27 11:31:24
我想在我的程序中实现这样的功能:
当用户调整系统音量时,我的程序中的音量值能够随之动态的改变。

请问,该如何实现?

我想到的方法有三个:
1)定时器。这个简单,我会。
2)截获系统消息。这个虽然晓得,但是做不来,不知道该截获哪些消息,请指教。
3)特定的API函数。不知道有没有。-_-b

...全文
163 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackLucifer 2004-01-09
我晕
楼上的,请看请我的问题好么?
我并不想知道怎么在自己的程序中调整系统音量的数值啊。
这么说吧,我已经在程序中实现了调整系统音量的方法,用的正是你所写的这个方法,但是,请你把操作系统的音量控制面板,然后在你的程序中调整音量,你会发现操作系统的音量控制面板中的音量指示器(也就是那个滑块)会自动调整,但反过来,你再操作系统的音量控制面板中调整音量之后,你的程序却不会及时反映。
而这才是我想实现的功能啊。
不知你对此有何好方法?
回复
Friecin 2004-01-05
//----------------------------设定音量---------------------------------------

bool GetVolumeControl(HMIXER hmixer ,long componentType,long ctrlType,MIXERCONTROL* mxc)
{
MIXERLINECONTROLS mxlc;
MIXERLINE mxl;
mxl.cbStruct = sizeof(mxl);
mxl.dwComponentType = componentType;
if(!mixerGetLineInfo((HMIXEROBJ)hmixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE))
{
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = ctrlType;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(MIXERCONTROL);
mxlc.pamxctrl = mxc;
if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE))
return 0;
else
return 1;
}
return 0;
}
//---------------------------------------------------------------------------
long GetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if (mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return mxcdMute.fValue;
}

//---------------------------------------------------------------------------
unsigned GetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol; vol.dwValue=0;
mxcd.hwndOwner = 0;
mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerGetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))
return -1;
return vol.dwValue;
}

//---------------------------------------------------------------------------
bool SetMuteValue(HMIXER hmixer ,MIXERCONTROL *mxc, bool mute)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcdMute;mxcdMute.fValue=mute;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(mxcdMute);
mxcd.paDetails = &mxcdMute;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
if (mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return 0;
return 1;
}

//---------------------------------------------------------------------------

bool SetVolumeValue(HMIXER hmixer ,MIXERCONTROL *mxc, long volume)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_UNSIGNED vol;vol.dwValue = volume;
mxcd.hwndOwner = 0;
mxcd.dwControlID = mxc->dwControlID;
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(vol);
mxcd.paDetails = &vol;
mxcd.cChannels = 1;
if(mixerSetControlDetails((HMIXEROBJ)hmixer, &mxcd, MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))
return 0;
return 1;
}


//---------------------------------------------------------------------------
unsigned WINAPI GetVolume(int dev)//得到设备的音量dev=0主音量,1WAVE ,2MIDI ,3 LINE IN
{

long device;unsigned rt=0;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
// device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break; // cd 音量
// device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; //麦克风音量
// device=MIXERLINE_COMPONENTTYPE_SRC_LINE; break; //PC 扬声器音量
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;

default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}

if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(!GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))
return 0;
rt=GetVolumeValue(hmixer,&volCtrl)*100/volCtrl.Bounds.lMaximum;
mixerClose(hmixer);
return rt;

}

//---------------------------------------------------------------------------

bool WINAPI SetVolume(long dev,long vol)//设置设备的音量
{
// dev =0,1,2 分别表示主音量,波形,MIDI ,LINE IN
// vol=0-100 表示音量的大小 , 设置与返回音量的值用的是百分比,即音量从0 - 100,而不是设备的绝对值
// retrun false 表示设置音量的大小的操作不成功
// retrun true 表示设置音量的大小的操作成功

long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;

default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}

if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;

if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))
{
vol=vol*volCtrl.Bounds.lMaximum/100;
if(SetVolumeValue(hmixer,&volCtrl,vol))
rc=true;
}
mixerClose(hmixer);
return rc;
}

//---------------------------------------------------------------------------

bool WINAPI SetMute(long dev,long vol)//设置设备静音
{
// dev =0,1,2 分别表示主音量,波形,MIDI ,LINE IN
// vol=0,1 分别表示取消静音,设置静音
// retrun false 表示取消或设置静音操作不成功
// retrun true 表示取消或设置静音操作成功

long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;

default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}

if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;
if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))
if(SetMuteValue(hmixer,&volCtrl,(bool)vol))
rc=true;
mixerClose(hmixer);
return rc;
}

//---------------------------------------------------------------------------

bool WINAPI GetMute(long dev)//检查设备是否静音
{
//dev =0,1,2 分别表示主音量,波形,MIDI ,LINE IN
// retrun false 表示没有静音
// retrun true 表示静音
long device;
bool rc=false;
MIXERCONTROL volCtrl;
HMIXER hmixer;
switch (dev)
{
case 1:
device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break;
case 2:
device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;
case 3:
device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC; break;
default:
device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
}

if(mixerOpen(&hmixer, 0, 0, 0, 0)) return 0;

if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))
rc=GetMuteValue(hmixer,&volCtrl);
mixerClose(hmixer);
return rc;

}


使用:
//调节音量
void __fastcall TForm1::TrackBar0Change(TObject *Sender)
{
SetVolume(0,TrackBar0->Position*10);
}

//静音
void __fastcall TForm1::CheckBox0Click(TObject *Sender)
{
SetMute(0,CheckBox0->Checked);
}
回复
JackLucifer 2004-01-05
我去试试看
不好意思
前两天加班的说
没来看
回复
zjffq 2003-12-28
有个控件MMTOOLS的很容易实现
回复
NowCan 2003-12-28
MM_MIXM_CONTROL_CHANGE
是不是这个?截获消息后再读取一下当前值
回复
NowCan 2003-12-28
有消息的,我也忘了是什么了,明天去找找。
回复
发动态
发帖子
Windows SDK/API
创建于2007-08-02

1202

社区成员

C++ Builder Windows SDK/API
申请成为版主
社区公告
暂无公告