1、有注册表保存, 系统启动后默认音量
注册表设置
; Sound CPL registry value
[HKEY_CURRENT_USER/ControlPanel/Volume]
"Key"=dword:10002 ; Keyclicks: 1=soft, 0x10002=loud
"Screen"=dword:10002 ; ScreenTaps: 1=soft, 0x10002=loud
"Mute"=dword:7 ; 7=Enable all sounds (1=Notifications, 2=Apps, 4=Events.)
"Volume"=dword:ffffffff ; 0=off, 0xFFFFFFFF=maximum
2、系统临时改变系统音量, 但不修改系统当前音量。
void SetSoundVolume(DWORD dwVolume) {
WAVEFORMATEX wf;
wf.wFormatTag = WAVE_FORMAT_PCM;
wf.nChannels = 1;
wf.nSamplesPerSec = 8000 * 1000;
wf.wBitsPerSample = 8;
wf.nBlockAlign = wf.nChannels * wf.wBitsPerSample / 8;
wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
wf.cbSize = 0;
HWAVEOUT hwo;
for (UINT id = 0; id < waveOutGetNumDevs(); id++) {
if (waveOutOpen(&hwo, id, &wf, 0, 0, CALLBACK_NULL) == MMSYSERR_NOERROR)
{
waveOutSetVolume(hwo, dwVolume);
waveOutClose(hwo);
}
}
}
dwVolume的值是0x0---0xffff,如果你想使用百分比设置音量,而不使用0到0xFFFF范围,那么你可以用下面的代码:
DWORD dwSoundVolumePercents = 50;
SetSoundVolume( (DWORD)(0xFFFF*1.0*dwSoundVolumePercents/100.0) );
3、修改系统的音量(主设备:0)但又不会和系统其他音频设备发生操作冲突.
waveOutSetVolume(0, dwVolume);
有时候使用:waveOutSetVolume(hwo, dwVolume); 会发生打开设备冲突。
4、如果要实时保存已经修改了的音量,必须结合注册表保存,并且必须刷新注册表。
转自Tony嵌入式 http://Cevx.Com http://Cevx.Net