怎么去系统总音量是否静音?
怎么控制总音量静音?

解决方案 »

  1.   

    unit Unit2;interfaceuses MMSystem, Dialogs;Type
      TDeviceType = (dtMaster, dtMicrophone, dtWaveOut, dtSynth,dtCD);
    //主音量,MIC,Wave输出,MIDI输出,CD输出.
      TValueType = (vtVolume, vtMute, vtDeviceName);
      TAUDIODevice=Class
      private
        FDeviceType:TDeviceType;
        FDeviceName:String;
        procedure SetMute(const Value: Boolean);
        procedure SetVolume(const Value: Word);
        function GetValue(ValueType:TValueType): Word;
        function GetVolume: Word;
        function GetMute: Boolean;
        procedure SetValue(Value: Word;ValueType:TValueType);
        function GetDeviceName: String;
      Public
        Property DeviceType:TDeviceType read FDeviceType write FDeviceType;
        Property DeviceName:String Read GetDeviceName;
        Property Volume:Word Read GetVolume Write SetVolume;
        Property Mute:Boolean Read GetMute Write SetMute;
      end;const DeviceTypeName:Array [TDeviceType] of String = ('Master', 'Microphone', 'WaveOut', 'Synth','CD');implementation
    Uses sysutils;
    { TAUDIODevice }//设置音量或者静音
    procedure TAUDIODevice.SetValue(Value : Word;ValueType:TValueType);
    var
      PriMixer: HMIXER;
      PriMixLineControl: MIXERLINECONTROLS;
      PriMixerControlDetails: TMIXERCONTROLDETAILS;
      PriMixerControlDetails_Unsigned: TMIXERCONTROLDETAILS_UNSIGNED;
      PriMixerControl: MIXERCONTROL;
      PriMixerLine: TMixerLine;
      iReturn: Integer;
    begin
      if (mixerGetNumDevs() < 1) then
        Exit;
      if ValueType=vtDeviceName then
        Exit;  iReturn := mixerOpen(@PriMixer, 0, 0, 0, 0);
      if iReturn = MMSYSERR_NOERROR then
      begin
        case FDeviceType of
          dtMaster     :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
          dtMicrophone :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
          dtWaveOut    :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
          dtSynth      :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
          dtCD         :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
        end;
        PriMixerLine.cbStruct := SizeOf(PriMixerLine);    iReturn := mixerGetLineInfo(PriMixer, @PriMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);    if iReturn = MMSYSERR_NOERROR then
        begin
          FillChar(PriMixLineControl, SizeOf(PriMixLineControl),0);
          PriMixLineControl.cbStruct := SizeOf(PriMixLineControl);
          PriMixLineControl.dwLineID := PriMixerLine.dwLineID;
          if ValueType=vtMute then
            PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE
          else if ValueType=vtVolume then
            PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
          PriMixLineControl.cControls := 1;
          PriMixLineControl.cbmxctrl := SizeOf(PriMixerControl);      PriMixLineControl.pamxctrl := @PriMixerControl;
          iReturn := mixerGetLineControls(PriMixer, @PriMixLineControl, MIXER_GETLINECONTROLSF_ONEBYTYPE);      if iReturn = MMSYSERR_NOERROR then
          begin
            FillChar(PriMixerControlDetails, SizeOf(PriMixerControlDetails),0);
            PriMixerControlDetails.dwControlID := PriMixerControl.dwControlID;
            PriMixerControlDetails.cbStruct := SizeOf(PriMixerControlDetails);
            PriMixerControlDetails.cMultipleItems := 0;
            PriMixerControlDetails.cbDetails := SizeOf(PriMixerControlDetails_Unsigned);
            PriMixerControlDetails.paDetails := @PriMixerControlDetails_Unsigned;
            PriMixerControlDetails.cChannels := 1;        PriMixerControlDetails_Unsigned.dwValue := Value;        iReturn := mixerSetControlDetails(PriMixer, @PriMixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);        if iReturn <> MMSYSERR_NOERROR then
              Raise Exception.Create('Set Control Details Error!');
          end
          else
            Raise Exception.Create('Get Control Line Information Error!');
        end;
        mixerClose(PriMixer);
      end;
    end;function TAUDIODevice.GetValue(ValueType:TValueType): Word;
    var
      PriMixer: HMIXER;
      PriMixLineControl: MIXERLINECONTROLS;
      PriMixerControlDetails: TMIXERCONTROLDETAILS;
      PriMixerControlDetails_Unsigned: TMIXERCONTROLDETAILS_UNSIGNED;
      PriMixerControl: MIXERCONTROL;
      PriMixerLine: TMixerLine;
      iReturn: Integer;
    begin
      Result:=0;
      //检查是否有相关设备.
      FDeviceName:='';
      if (mixerGetNumDevs() < 1) then
        Exit;  //打开Mixer设备.
      iReturn := mixerOpen(@PriMixer, 0, 0, 0, 0);
      if iReturn = MMSYSERR_NOERROR then
      begin
        //初始化参数.
        case FDeviceType of
          dtMaster     :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
          dtMicrophone :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
          dtWaveOut    :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
          dtSynth      :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
          dtCD         :  PriMixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
        end;
        PriMixerLine.cbStruct := SizeOf(PriMixerLine);    //得到设备信息.
        iReturn := mixerGetLineInfo(PriMixer, @PriMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);    if iReturn = MMSYSERR_NOERROR then
        //返回正常.
        begin
          if ValueType=vtDeviceName then
            FDeviceName:=PriMixerLine.szName
          else
          begin
            FillChar(PriMixLineControl, SizeOf(MIXERLINECONTROLS),0);
            //清零.
            PriMixLineControl.cbStruct := SizeOf(PriMixLineControl);
            PriMixLineControl.dwLineID := PriMixerLine.dwLineID;
            if ValueType=vtMute then
              PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE
            else if ValueType=vtVolume then
              PriMixLineControl.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
            PriMixLineControl.cControls := 1;
            PriMixLineControl.cbmxctrl := SizeOf(MIXERCONTROL);        PriMixLineControl.pamxctrl := @PriMixerControl;
            iReturn := mixerGetLineControls(PriMixer, @PriMixLineControl, MIXER_GETLINECONTROLSF_ONEBYTYPE);        if iReturn = MMSYSERR_NOERROR then
            begin
              FillChar(PriMixerControlDetails, SizeOf(PriMixerControlDetails),0);
              PriMixerControlDetails.dwControlID := PriMixerControl.dwControlID;
              PriMixerControlDetails.cbStruct := SizeOf(PriMixerControlDetails);
              PriMixerControlDetails.cMultipleItems := 0;
              PriMixerControlDetails.cbDetails := SizeOf(PriMixerControlDetails_Unsigned);
              PriMixerControlDetails.paDetails := @PriMixerControlDetails_Unsigned;
              PriMixerControlDetails.cChannels := 1;          //得到详细信息.
              iReturn := mixerGetControlDetails(PriMixer, @PriMixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);          //音量.
              Result := PriMixerControlDetails_Unsigned.dwValue;          if iReturn <> MMSYSERR_NOERROR then
                Raise Exception.Create('Get Control Details Error!');
            end
            else
              Raise Exception.Create('Get Control Line Information Error!');
          end;
        end;
        mixerClose(PriMixer);
        //关闭.
      end;
    end;function TAUDIODevice.GetMute: Boolean;
    begin
      Result:=GetValue(vtMute)<>0;
    end;procedure TAUDIODevice.SetMute(const Value: Boolean);
    begin
      SetValue(ABS(ORD(Value)),vtMute);
    end;procedure TAUDIODevice.SetVolume(const Value: Word);
    begin
      SetValue(Value,vtVolume);
    end;function TAUDIODevice.GetVolume: Word;
    begin
      Result:=GetValue(vtVolume);
    end;function TAUDIODevice.GetDeviceName: String;
    begin
      GetValue(vtDeviceName);
      Result:=FDeviceName;
    end;
      

  2.   

    贴少了 个 "end."再最后,自己注意一下
      

  3.   

    http://www.delphiun.com/down_view.asp?id=1803
    http://www.delphibox.com/article.asp?articleid=1158
    http://www.coderpub.com/View.aspx?fbId=5&Id=2341
      

  4.   

    用Mixer控制API即可
    上面都说了的