9908006 发表于 2010-9-24 22:14:08

delphi音量控制程序

本帖最后由 9908006 于 2010-9-25 18:18 编辑

寝室有一哥们是夜猫子,白天睡得不起来,晚上龙精虎猛,熬夜上网,虽然没开音箱,但是耳机漏音严重,而本人睡觉对环境要求很高,没办法,弄了个这玩弄放到他电脑里,再弄个settimer,每秒循环一次,从此世界清净很多了
unit funVolume;

interface

uses MMSystem;

type TDeviceName = (Master, Microphone, WaveOut, Synth);

function GetVolume(DN: TDeviceName): Word;
procedure SetVolume(DN: TDeviceName; Value: Word);
function GetVolumeMute(DN: TDeviceName): Boolean;
procedure SetVolumeMute(DN: TDeviceName; Value: Boolean);

implementation

//获取音量

function GetVolume(DN: TDeviceName): Word;
var
hMix: HMIXER;
mxlc: MIXERLINECONTROLS;
mxcd: TMIXERCONTROLDETAILS;
vol: TMIXERCONTROLDETAILS_UNSIGNED;
mxc: MIXERCONTROL;
mxl: TMixerLine;
intRet: Integer;
nMixerDevs: Integer;
begin
// Check if Mixer is available
nMixerDevs := mixerGetNumDevs();
if (nMixerDevs < 1) then
begin
    Exit;
end;

// open the mixer
intRet := mixerOpen(@hMix, 0, 0, 0, 0);
if intRet = MMSYSERR_NOERROR then
begin
    case DN of
      Master: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
      Microphone:
      mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
      WaveOut: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
      Synth: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
    end;
    mxl.cbStruct := SizeOf(mxl);

   // get line info
    intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

    if intRet = MMSYSERR_NOERROR then
    begin
      FillChar(mxlc, SizeOf(mxlc), 0);
      mxlc.cbStruct := SizeOf(mxlc);
      mxlc.dwLineID := mxl.dwLineID;
      mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
      mxlc.cControls := 1;
      mxlc.cbmxctrl := SizeOf(mxc);

      mxlc.pamxctrl := @mxc;
      intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

      if intRet = MMSYSERR_NOERROR then
      begin
      FillChar(mxcd, SizeOf(mxcd), 0);
      mxcd.dwControlID := mxc.dwControlID;
      mxcd.cbStruct := SizeOf(mxcd);
      mxcd.cMultipleItems := 0;
      mxcd.cbDetails := SizeOf(Vol);
      mxcd.paDetails := @vol;
      mxcd.cChannels := 1;

      intRet := mixerGetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

      Result := vol.dwValue;


      end
      else

    end;
    intRet := mixerClose(hMix);
end;
end;

//设置音量

procedure setVolume(DN: TDeviceName; Value: Word);
var
hMix: HMIXER;
mxlc: MIXERLINECONTROLS;
mxcd: TMIXERCONTROLDETAILS;
vol: TMIXERCONTROLDETAILS_UNSIGNED;
mxc: MIXERCONTROL;
mxl: TMixerLine;
intRet: Integer;
nMixerDevs: Integer;
begin
// Check if Mixer is available
nMixerDevs := mixerGetNumDevs();
if (nMixerDevs < 1) then
begin
    Exit;
end;

// open the mixer
intRet := mixerOpen(@hMix, 0, 0, 0, 0);
if intRet = MMSYSERR_NOERROR then
begin
    case DN of
      Master: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
      Microphone:
      mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
      WaveOut: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
      Synth: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
    end;
    mxl.cbStruct := SizeOf(mxl);

   // get line info
    intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

    if intRet = MMSYSERR_NOERROR then
    begin
      FillChar(mxlc, SizeOf(mxlc), 0);
      mxlc.cbStruct := SizeOf(mxlc);
      mxlc.dwLineID := mxl.dwLineID;
      mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
      mxlc.cControls := 1;
      mxlc.cbmxctrl := SizeOf(mxc);

      mxlc.pamxctrl := @mxc;
      intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

      if intRet = MMSYSERR_NOERROR then
      begin
      FillChar(mxcd, SizeOf(mxcd), 0);
      mxcd.dwControlID := mxc.dwControlID;
      mxcd.cbStruct := SizeOf(mxcd);
      mxcd.cMultipleItems := 0;
      mxcd.cbDetails := SizeOf(Vol);
      mxcd.paDetails := @vol;
      mxcd.cChannels := 1;

      vol.dwValue := Value;

      intRet := mixerSetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);


      end
      else

    end;
    intRet := mixerClose(hMix);
end;
end;

//获取静音

function GetVolumeMute(DN: TDeviceName): Boolean;
var
hMix: HMIXER;
mxlc: MIXERLINECONTROLS;
mxcd: TMIXERCONTROLDETAILS;
vol: TMIXERCONTROLDETAILS_UNSIGNED;
mxc: MIXERCONTROL;
mxl: TMixerLine;
intRet: Integer;
nMixerDevs: Integer;
mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
// Check if Mixer is available
nMixerDevs := mixerGetNumDevs();
if (nMixerDevs < 1) then
begin
    Exit;
end;

// open the mixer
intRet := mixerOpen(@hMix, 0, 0, 0, 0);
if intRet = MMSYSERR_NOERROR then
begin
    case DN of
      Master: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
      Microphone:
      mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
      WaveOut: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
      Synth: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
    end;
    mxl.cbStruct := SizeOf(mxl);

   // mixerline info
    intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

    if intRet = MMSYSERR_NOERROR then
    begin
      FillChar(mxlc, SizeOf(mxlc), 0);
      mxlc.cbStruct := SizeOf(mxlc);
      mxlc.dwLineID := mxl.dwLineID;
      mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
      mxlc.cControls := 1;
      mxlc.cbmxctrl := SizeOf(mxc);
      mxlc.pamxctrl := @mxc;

   // Get the mute control
      intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

      if intRet = MMSYSERR_NOERROR then
      begin
      FillChar(mxcd, SizeOf(mxcd), 0);
      mxcd.cbStruct := SizeOf(TMIXERCONTROLDETAILS);
      mxcd.dwControlID := mxc.dwControlID;
      mxcd.cChannels := 1;
      mxcd.cbDetails := SizeOf(MIXERCONTROLDETAILS_BOOLEAN);
      mxcd.paDetails := @mcdMute;

       // Getmute
      intRet := mixerGetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

      if mcdMute.fValue = 0 then Result := false
      else Result := True;


      end
      else

    end;

    intRet := mixerClose(hMix);
end;
end;

//设置静音

procedure SetVolumeMute(DN: TDeviceName; Value: Boolean);
var
hMix: HMIXER;
mxlc: MIXERLINECONTROLS;
mxcd: TMIXERCONTROLDETAILS;
vol: TMIXERCONTROLDETAILS_UNSIGNED;
mxc: MIXERCONTROL;
mxl: TMixerLine;
intRet: Integer;
nMixerDevs: Integer;
mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
// Check if Mixer is available
nMixerDevs := mixerGetNumDevs();
if (nMixerDevs < 1) then
begin
    Exit;
end;

// open the mixer
intRet := mixerOpen(@hMix, 0, 0, 0, 0);
if intRet = MMSYSERR_NOERROR then
begin
    case DN of
      Master: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
      Microphone:
      mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
      WaveOut: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
      Synth: mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
    end;
    mxl.cbStruct := SizeOf(mxl);

   // mixerline info
    intRet := mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

    if intRet = MMSYSERR_NOERROR then
    begin
      FillChar(mxlc, SizeOf(mxlc), 0);
      mxlc.cbStruct := SizeOf(mxlc);
      mxlc.dwLineID := mxl.dwLineID;
      mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
      mxlc.cControls := 1;
      mxlc.cbmxctrl := SizeOf(mxc);
      mxlc.pamxctrl := @mxc;

   // Get the mute control
      intRet := mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);

      if intRet = MMSYSERR_NOERROR then
      begin
      FillChar(mxcd, SizeOf(mxcd), 0);
      mxcd.cbStruct := SizeOf(TMIXERCONTROLDETAILS);
      mxcd.dwControlID := mxc.dwControlID;
      mxcd.cChannels := 1;
      mxcd.cbDetails := SizeOf(MIXERCONTROLDETAILS_BOOLEAN);
      mxcd.paDetails := @mcdMute;

       // Set and UnSetmute
      mcdMute.fValue := Ord(Value);
      intRet := mixerSetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

      end
      else

    end;

    intRet := mixerClose(hMix);
end;
end;

end.



调用如下,最好弄个settimer
program minvol;

uses
Windows,funVolume;


begin
SetVolume(Master,10000);
SetVolume(WaveOut,20000);
SetVolume(Microphone,20000);
end.

oopww 发表于 2010-9-25 13:15:34

我们寝室也有个额啊!
电脑有密码怎么搞啊!嘎嘎····

oopww 发表于 2010-9-25 13:18:24

我觉得最好是把Windows Audio这个服务给他停止了!哈哈·····
世界安静了,河蟹了!
哈哈·····

9908006 发表于 2010-9-25 18:18:46

设了密码?只要不是开机密码,就用pe进去,如果是开机密码,就强行放电呗
页: [1]
查看完整版本: delphi音量控制程序