How to get master volume in windows xp?

In Windows XP, with Delphi, how to get the main volume?

I know that I can configure and reset the keystrokes with keybd_event(VK_VOLUME_UP, 1, 0, 0);and keybd_event(VK_VOLUME_DOWN, 1, 0, 0);, but I do not know how to get the actual value of the volume.

+5
source share
1 answer

Below is a small modification of the example code found here (attributed to Thomas Stutz). This example sets the microphone volume. I just changed the type of component - the purpose of the speaker instead of the microphone source, and replaced it mixerSetControlDetailswith mixerGetControlDetailsand turned the setter into a receiver of course. On several systems that I tested here (XPSp3, XPSp2, W2K, 98), it works. The return function is the speaker from the first (default) mixer - the value is 0-65535, "ShowMessage" in the button handler changes it by a percentage. But do not ask me more about this, I really have no experience with the api mixer. Instead, write fi here , although the old article did seem to me to be comprehensive.

function GetSpeakerVolume(var bValue: Word): Boolean;
var                          {0..65535}
  hMix: HMIXER;
  mxlc: MIXERLINECONTROLS;
  mxcd: TMIXERCONTROLDETAILS;
  vol: TMIXERCONTROLDETAILS_UNSIGNED;
  mxc: MIXERCONTROL;
  mxl: TMixerLine;
  intRet: Integer;
  nMixerDevs: Integer;
begin
  Result := False;

  // Check if Mixer is available
  nMixerDevs := mixerGetNumDevs();
  if (nMixerDevs < 1) then
    Exit;

  // open the mixer
  intRet := mixerOpen(@hMix, 0, 0, 0, 0);
  if intRet = MMSYSERR_NOERROR then
  begin
    mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
    mxl.cbStruct := SizeOf(mxl);

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

    if intRet = MMSYSERR_NOERROR then
    begin
      ZeroMemory(@mxlc, SizeOf(mxlc));
      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
        ZeroMemory(@mxcd, SizeOf(mxcd));
        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_GETCONTROLDETAILSF_VALUE);
        if intRet <> MMSYSERR_NOERROR then
          ShowMessage('GetControlDetails Error')
        else begin
          bValue := vol.dwValue;
          Result := True;
        end;
      end
      else
        ShowMessage('GetLineInfo Error');
    end;
    intRet := mixerClose(hMix);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Vol: Word;
begin
  if GetSpeakerVolume(Vol) then
    ShowMessage(IntToStr(Round(Vol * 100 / 65535)));
end;
+3

All Articles