AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Get/Set Master Volume Lautstärke?

Get/Set Master Volume Lautstärke?

Ein Thema von PeterPanino · begonnen am 14. Okt 2023 · letzter Beitrag vom 16. Okt 2023
Antwort Antwort
Kas Ob.

Registriert seit: 3. Sep 2023
389 Beiträge
 
#1

AW: Get/Set Master Volume Lautstärke?

  Alt 14. Okt 2023, 16:05
My two cents on this, Mute and Enable are intentionally undocumented by Microsoft, these will compromise the user privacy, like .. by simply enabling the disabled microphone in first place, or just unmute the speakers without the user consent and play some audio.

It is doable and it is not a hack, but unethical to share, so you have to ask the user himself to enable (eg. the Mic) or unmute the speakers.
Kas
  Mit Zitat antworten Zitat
PeterPanino

Registriert seit: 4. Sep 2004
1.472 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Get/Set Master Volume Lautstärke?

  Alt 14. Okt 2023, 17:56
My two cents on this, Mute and Enable are intentionally undocumented by Microsoft, these will compromise the user privacy, like .. by simply enabling the disabled microphone in first place, or just unmute the speakers without the user consent and play some audio.

It is doable and it is not a hack, but unethical to share, so you have to ask the user himself to enable (eg. the Mic) or unmute the speakers.
In fact, that's what I am planning: The user should be able to simply click a button on my app to quickly mute or unmute his master volume without having to awkwardly call the Windows settings each time. With my apps, I always strive to make the operation of Windows easier, which is often unnecessarily complicated on many advanced settings. One usage example is: quickly switching off the sound while annoying commercials are playing in a movie. The user should always be in control of his computer's sound!
Geändert von PeterPanino, damit der Platz auf dem Bildschirm nicht so leer aussieht.

Geändert von PeterPanino (14. Okt 2023 um 17:58 Uhr)
  Mit Zitat antworten Zitat
tomkupitz

Registriert seit: 26. Jan 2011
351 Beiträge
 
Delphi 12 Athens
 
#3

AW: Get/Set Master Volume Lautstärke?

  Alt 14. Okt 2023, 19:21
gefunden...

Code:
uses MMSystem;

...

procedure GetWaveVolume(var volL, volR: DWord);
var
  hWO: HWAVEOUT;
  waveF: TWAVEFORMATEX;
  vol: DWORD;
begin
  volL:= 0;
  volR:= 0;
  // init TWAVEFORMATEX
  FillChar(waveF, SizeOf(waveF), 0);
  // open WaveMapper = std output of playsound
  waveOutOpen(@hWO, WAVE_MAPPER, @waveF, 0, 0, 0);
  // get volume
  waveOutGetVolume(hWO, @vol);
  volL:= vol and $FFFF;
  volR:= vol shr 16;
  waveOutClose(hWO);
end;

procedure SetWaveVolume(const volL, volR: DWord);
var
  hWO: HWAVEOUT;
  waveF: TWAVEFORMATEX;
  vol: DWORD;
begin
  // init TWAVEFORMATEX
  FillChar(waveF, SizeOf(waveF), 0);
  // open WaveMapper = std output of playsound
  waveOutOpen(@hWO, WAVE_MAPPER, @waveF, 0, 0, 0);
  vol:= volL + volR shl 16;
  // set volume
  waveOutSetVolume(hWO, vol);
  waveOutClose(hWO);
end;
  Mit Zitat antworten Zitat
PeterPanino

Registriert seit: 4. Sep 2004
1.472 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: Get/Set Master Volume Lautstärke?

  Alt 14. Okt 2023, 20:11
gefunden...

Code:
uses MMSystem;

...

procedure GetWaveVolume(var volL, volR: DWord);
var
  hWO: HWAVEOUT;
  waveF: TWAVEFORMATEX;
  vol: DWORD;
begin
  volL:= 0;
  volR:= 0;
  // init TWAVEFORMATEX
  FillChar(waveF, SizeOf(waveF), 0);
  // open WaveMapper = std output of playsound
  waveOutOpen(@hWO, WAVE_MAPPER, @waveF, 0, 0, 0);
  // get volume
  waveOutGetVolume(hWO, @vol);
  volL:= vol and $FFFF;
  volR:= vol shr 16;
  waveOutClose(hWO);
end;

procedure SetWaveVolume(const volL, volR: DWord);
var
  hWO: HWAVEOUT;
  waveF: TWAVEFORMATEX;
  vol: DWORD;
begin
  // init TWAVEFORMATEX
  FillChar(waveF, SizeOf(waveF), 0);
  // open WaveMapper = std output of playsound
  waveOutOpen(@hWO, WAVE_MAPPER, @waveF, 0, 0, 0);
  vol:= volL + volR shl 16;
  // set volume
  waveOutSetVolume(hWO, vol);
  waveOutClose(hWO);
end;
Diesem Irrtum bin ich vorher auch verfallen: Das betrifft nicht den Wert von MASTER VOLUME, sondern den Volume-Wert des eigenen Programms.

Kannst du in Settings -> App volume and device preferences genau beobachten.

Danke jedenfalls für deinen Beitrag! Much appreciated!
Geändert von PeterPanino, damit der Platz auf dem Bildschirm nicht so leer aussieht.

Geändert von PeterPanino (14. Okt 2023 um 20:32 Uhr)
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
389 Beiträge
 
#5

AW: Get/Set Master Volume Lautstärke?

  Alt 15. Okt 2023, 11:11
The user should always be in control of his computer's sound!
Sure but Windows lack ability to do permissions like iOS or Android so they revert to prohibit some API without explicitly user inatraction.

I was mistaken with the mute status, my old project was on specific request from a client where he wanted to control the devices in Sound Control Panel to enable and disable a device and change the Default device for both Playback and Recording, which is not allowed without user interaction, it took me hours of debugging to extract the Windows internal code which was one freaking line!!

Anyway i tried this based on your code and it is working just fine
Code:
procedure TForm10.SetMasterMuteState(Muted: Boolean);
var
  MuteStatus: Boolean;
  FAudioEndpointVolume: AudioEndpoint.IAudioEndpointVolume; // from the Andreas Rejbrand unit
  FDeviceEnumerator: IMMDeviceEnumerator;
  FMMDevice: IMMDevice;
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
    if not Succeeded(FAudioEndpointVolume.SetMute(Muted, nil)) then
      OutputDebugString(PChar('TForm1.SetMasterVolumeLevelScalar: SetMute OK ' + BoolToStr
        (Muted, True)))
    else
      OutputDebugString(PChar('TForm1.SetMasterVolumeLevelScalar: SetMute Fail ' +
        BoolToStr(Muted, True)))
  end
  else
  begin
    OutputDebugString('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
      //CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
  end;
end;
It is working on my Windows 10 (1803)
Kas
  Mit Zitat antworten Zitat
PeterPanino

Registriert seit: 4. Sep 2004
1.472 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Get/Set Master Volume Lautstärke?

  Alt 15. Okt 2023, 13:06
The user should always be in control of his computer's sound!
Sure but Windows lack ability to do permissions like iOS or Android so they revert to prohibit some API without explicitly user inatraction.

I was mistaken with the mute status, my old project was on specific request from a client where he wanted to control the devices in Sound Control Panel to enable and disable a device and change the Default device for both Playback and Recording, which is not allowed without user interaction, it took me hours of debugging to extract the Windows internal code which was one freaking line!!

Anyway i tried this based on your code and it is working just fine
Code:
...
    if not Succeeded(FAudioEndpointVolume.SetMute(Muted, nil)) then
      OutputDebugString(PChar('TForm1.SetMasterVolumeLevelScalar: SetMute OK ' + BoolToStr
        (Muted, True)))
    else
      OutputDebugString(PChar('TForm1.SetMasterVolumeLevelScalar: SetMute Fail ' +
        BoolToStr(Muted, True)))
...
It is working on my Windows 10 (1803)
Isn't the logic inverted here? (if not Succeeded -> SetMute OK)

Anyway - when calling with a False parameter, checking the exact HRESULT it returns FALSE here:
Code:
    var R: System.HResult := FAudioEndpointVolume.SetMute(Muted, nil);
    case R of
      S_OK:         CodeSite.Send('TForm1.SetMasterVolumeMuteState: OK');         // successful operation with a return value of True
      S_FALSE:      CodeSite.Send('TForm1.SetMasterVolumeMuteState: FALSE');      // successful operation with a return value of False
      E_NOINTERFACE: CodeSite.Send('TForm1.SetMasterVolumeMuteState: NOINTERFACE'); // Interface not supported
      E_UNEXPECTED: CodeSite.Send('TForm1.SetMasterVolumeMuteState: UNEXPECTED'); // Catastrophic failure
      E_NOTIMPL:    CodeSite.Send('TForm1.SetMasterVolumeMuteState: NOTIMPL');    // Operation not implemented
    end;
Windows Version: 10.0 Build 19045, Windows 10 (Version 22H2, OS Build 19045.3570, 64-bit Edition)
Geändert von PeterPanino, damit der Platz auf dem Bildschirm nicht so leer aussieht.

Geändert von PeterPanino (15. Okt 2023 um 13:41 Uhr)
  Mit Zitat antworten Zitat
PeterPanino

Registriert seit: 4. Sep 2004
1.472 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: Get/Set Master Volume Lautstärke?

  Alt 15. Okt 2023, 13:43
Debugging it further:

Delphi-Quellcode:
procedure TForm1.SetMasterVolumeMuteState(Muted: Boolean);
var
  MuteStatus: Boolean;
  FAudioEndpointVolume: AudioEndpoint.IAudioEndpointVolume; // from the Andreas Rejbrand unit
  FDeviceEnumerator: IMMDeviceEnumerator;
  FMMDevice: IMMDevice;
begin
  CodeSite.Send('TForm1.SetMasterVolumeMuteState: A');
  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;

  CodeSite.Send('TForm1.SetMasterVolumeMuteState: B');

  if Assigned(FAudioEndpointVolume) then
  begin
    CodeSite.Send('TForm1.SetMasterVolumeMuteState: C');
    var R: System.HResult := FAudioEndpointVolume.SetMute(Muted, nil);
    case R of
      S_OK: CodeSite.Send('TForm1.SetMasterVolumeMuteState: OK'); // successful operation with a return value of True
      S_FALSE: CodeSite.Send('TForm1.SetMasterVolumeMuteState: FALSE'); // successful operation with a return value of False
      E_NOINTERFACE: CodeSite.Send('TForm1.SetMasterVolumeMuteState: NOINTERFACE'); // Interface not supported
      E_UNEXPECTED: CodeSite.Send('TForm1.SetMasterVolumeMuteState: UNEXPECTED'); // Catastrophic failure
      E_NOTIMPL: CodeSite.Send('TForm1.SetMasterVolumeMuteState: NOTIMPL'); // Operation not implemented
      else CodeSite.Send('TForm1.SetMasterVolumeMuteState: ELSE');
    end;
  end
  else
  begin
    //OutputDebugString('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
    CodeSite.Send('TForm1.SetMasterVolumeLevelScalar: FAudioEndpointVolume not assigned!');
  end;
end;
When calling SetMasterVolumeMuteState(True);

I get these debug messages:

TForm1.SetMasterVolumeMuteState: A
TForm1.SetMasterVolumeMuteState: B
TForm1.SetMasterVolumeMuteState: C
TForm1.SetMasterVolumeMuteState: ELSE
Geändert von PeterPanino, damit der Platz auf dem Bildschirm nicht so leer aussieht.
  Mit Zitat antworten Zitat
Alt 5. Jun 2024, 05:24     Erstellt von rasads
Dieser Beitrag wurde von TBx gelöscht. - Grund: Verdacht auf SPAM und den damit verbundenen verschwenderischen Umgang von wertvollen Bits und Bytes
Antwort Antwort

 
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:18 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz