Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
346 Beiträge
 
#15

AW: Get/Set Master Volume Lautstärke?

  Alt 15. Okt 2023, 18:18
Here is my full working example
Code:
procedure TForm10.Button3Click(Sender: TObject);
begin
  SetMasterMuteState(True);
end;

procedure TForm10.Button4Click(Sender: TObject);
begin
  SetMasterMuteState(False);
end;

procedure TForm10.SetMasterMuteState(Muted: Boolean);
var
  MuteStatus: Boolean;
  FAudioEndpointVolume: AudioEndpoint.IAudioEndpointVolume; // from the Andreas Rejbrand unit
  FDeviceEnumerator: IMMDeviceEnumerator;
  FMMDevice: IMMDevice;
  R: System.HResult;
begin
  if not Succeeded(CoCreateInstance(CLASS_IMMDeviceEnumerator, nil, CLSCTX_INPROC_SERVER,
    IID_IMMDeviceEnumerator, FDeviceEnumerator)) then
  begin
    OutputDebugString('TForm1.SetMasterVolumeLevelScalar: 1');
      //CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: 1');
    ExitProcess(1);
  end;

  if not Succeeded(FDeviceEnumerator.GetDefaultAudioEndpoint(0, 0, FMMDevice)) then
  begin
    OutputDebugString('TForm1.SetMasterVolumeLevelScalar: 2');
      //CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: 2');
    ExitProcess(1);
  end;

  if not Succeeded(FMMDevice.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil,
    FAudioEndpointVolume)) then
  begin
    OutputDebugString('TForm1.SetMasterVolumeLevelScalar: 3');
      //CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: 3');
    ExitProcess(1);
  end;

  if Assigned(FAudioEndpointVolume) then
  begin

      R := FAudioEndpointVolume.SetMute(Muted, nil);
    case R of
      S_OK: OutputDebugString('TForm1.SetMasterVolumeMuteState: OK'); // successful operation with a return value of True
      S_FALSE: OutputDebugString('TForm1.SetMasterVolumeMuteState: FALSE'); // successful operation with a return value of False
      E_NOINTERFACE: OutputDebugString('TForm1.SetMasterVolumeMuteState: NOINTERFACE'); // Interface not supported
      E_UNEXPECTED: OutputDebugString('TForm1.SetMasterVolumeMuteState: UNEXPECTED'); // Catastrophic failure
      E_NOTIMPL: OutputDebugString('TForm1.SetMasterVolumeMuteState: NOTIMPL'); // Operation not implemented
      else OutputDebugString(PChar('TForm1.SetMasterVolumeMuteState: ELSE '+ IntToHex(R,8)));
    end;

  end
  else
  begin
    OutputDebugString('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
      //CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
  end;
end;
Kas
  Mit Zitat antworten Zitat