how to adjust master volume in vista/xp

前端 未结 4 782
我寻月下人不归
我寻月下人不归 2021-01-13 01:20

i want to adjust the volume programatically like Get/SetMasterVolume in vista and xp? using mmsystem unit?

4条回答
  •  情话喂你
    2021-01-13 02:01

    Windows XP:

    function SetMasterVolume(VolToSet: word; out VolSet: word): MMResult;
    var
      MixerHandle: HMixer;
      Volume: TMixerControlDetails_Unsigned;
      MixerLine: TMixerLine;
      MixerLineControls: TMixerLineControls;
      VolumeCtrl: TMixerControl;
      MixerControlDetails: TMixerControlDetails;
    begin
      // Get mixer handle
      Result := mixerOpen(@MixerHandle, 0, 0, 0, 0);
      if Result <> MMSYSERR_NOERROR then Exit;
      try
        // Get master volume line
        FillChar(MixerLine, SizeOf(TMixerLine), 0);
        MixerLine.cbStruct := SizeOf(TMixerLine);
        MixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
        Result := mixerGetLineInfo(MixerHandle, @MixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);
    
        if Result <> MMSYSERR_NOERROR then Exit;
        // Get the volume control of the master volume line
        FillChar(VolumeCtrl, SizeOf(TMixerControl), 0);
        MixerLineControls.cbStruct := SizeOf(TMixerLineControls);
        MixerLineControls.dwLineID := MixerLine.dwLineID;
        MixerLineControls.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
        MixerLineControls.cControls := 1;
        MixerLineControls.cbmxctrl := SizeOf(TMixerControl);
        MixerLineControls.pamxctrl := @VolumeCtrl;
        Result := mixerGetLineControls(MixerHandle,@MixerLineControls,MIXER_GETLINECONTROLSF_ONEBYTYPE);
    
        if Result <> MMSYSERR_NOERROR then Exit;
        // Set details (volume) for the volume control of the master volume line
        FillChar(MixerControlDetails, SizeOf(TMixerControlDetails), 0);
        MixerControlDetails.cbStruct := SizeOf(TMixerControlDetails);
        MixerControlDetails.dwControlID := VolumeCtrl.dwControlID;
        MixerControlDetails.cChannels := 1;
        MixerControlDetails.cMultipleItems := 0;
        MixerControlDetails.cbDetails := SizeOf(TMixerControlDetails_Unsigned);
        MixerControlDetails.paDetails := @Volume;
        Volume.dwValue := VolToSet;
        Result := mixerSetControlDetails(MixerHandle, @MixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);
      finally
        mixerClose(MixerHandle);
      end;
    end;
    

    And the call:

    var y:word;
    begin
      SetMasterVolume(2000,y);
    end;
    

提交回复
热议问题