How to check if the system master volume is mute or unmute?

前端 未结 3 571
旧巷少年郎
旧巷少年郎 2021-01-05 13:41

I\'m using this code to mute/unmute system master volume:

const
  APPCOMMAND_VOLUME_MUTE = $80000;
  WM_APPCOMMAND = $319;

procedure TForm1.Button1Click(Sen         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 14:24

    The GetMute method's parameter should be BOOL rather than Boolean. Likewise for SetMute. –

    Well, that is to say.. Yes and no.. Yes A Delphi BOOL (actually a LongBool) can store a C-BOOL. No, because it can't be used to write to a C-BOOL property. You will get a 0x80070057 = "Wrong Parameter" result.

    The simple reason is that In Delphi True means "everything but 0" and represents -1. A C-BOOL True, however represents 1 and only 1.

    So, using a LongBool doesn't work and you should use a workaround using an INT, LongInt, Integer or your own proper defined "BOOL" to avoid "Wrong Parameter" results.

    Here an example (that works in Delphi XE7 and tested with SDK version 10.0.10586.15:

    // Workaround for BOOL
    type TcBOOL = (cFalse = Integer(0),
                   cTrue = Integer(1));
    
    // IAudioEndpointVolume
    function SetMute(bMute: BOOL; pguidEventContext: PGUID): HRESULT; stdcall;
    function GetMute(out pbMute: BOOL): HRESULT; stdcall;
    
    
    // Client functions
    function TMfpMixer.GetMute(): TcBOOL;
    var
      hr: HResult;
      Res: BOOL;
    
    begin
      hr:= FAudioEndpoint.GetMute(Res);
    
      if FAILED(hr) then
        OleCheck(hr);
    
      Result:= TcBOOL(Res);
    end;
    
    //
    procedure TMfpMixer.SetMute(Value: TcBOOL);
    var
      hr: HResult;
    
    begin
      // This is a workaround on the Delphi BOOL issue. 
      hr:= FAudioEndpoint.SetMute(BOOL(Value),
                                  Nil);
      OleCheck(hr);
    end;
    

提交回复
热议问题