【问题标题】:How to get the master volume in windows xp?如何在 windows xp 中获取主音量?
【发布时间】:2011-05-29 03:27:02
【问题描述】:

在 Windows XP 中,使用 Delphi,如何获取主卷?

我知道我可以使用keybd_event(VK_VOLUME_UP, 1, 0, 0);keybd_event(VK_VOLUME_DOWN, 1, 0, 0); 设置上下发送击键,但我不知道如何获取音量的实际值。

【问题讨论】:

标签: delphi windows-xp multimedia


【解决方案1】:

下面是对here 的示例代码的一些修改(归功于Thomas Stutz)。那里的示例设置了麦克风音量。我只是修改了组件类型-扬声器目标而不是麦克风源,并将mixerSetControlDetails替换为mixerGetControlDetails,当然将setter变成了getter。在我在这里测试的几个系统(XPSp3、XPSp2、W2K、98)上,它似乎工作。该函数的返回是第一个(默认)混音器的扬声器 - 值 0-65535,按钮处理程序中的“ShowMessage”将其更改为百分比。但是不要问我更多的细节,我真的没有使用mixer api的经验。而是参考here f.i.,虽然旧文章对我来说确实很全面。

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;

【讨论】:

  • 我总是得到值 0。我使用 Windows 7
  • @opc0de - 查看问题的标题,eKek0 专门询问了适用于 XP 的代码。如果您阅读 cmets 对他的回答,您会发现他知道 Vista 和更高版本的情况有所不同。
猜你喜欢
  • 2011-03-27
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 2015-05-24
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多