Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   API de Windows (https://www.clubdelphi.com/foros/forumdisplay.php?f=7)
-   -   Volumen de mi PC (https://www.clubdelphi.com/foros/showthread.php?t=49280)

madiazg 17-10-2007 20:22:17

Volumen de mi PC
 
Hola, buenos días,
mediante este código puedo controlar el nivel del volumen:
Código:

uses
MMSystem;
function GetVolumeControl(aMixer: HMixer; componentType, ctrlType: Longint;
var mxc: TMixerControl): Boolean;
var
  mxl: TMixerLine;
  mxlc: TMixerLineControls;
  rc: Longint;
begin
  Result := False;
  FillChar(mxl, SizeOf(TMixerLine), 0);
  mxl.cbStruct := SizeOf(TMixerLine);
  mxl.dwComponentType := componentType;
{Obtain a line corresponding to the component type}
  rc := mixerGetLineInfo(aMixer, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);
  if rc = MMSYSERR_NOERROR then
  begin
    with mxlc do
    begin
      cbStruct := SizeOf(TMixerLineControls);
      dwLineID := mxl.dwLineID;
      dwControlType := ctrlType;
      cControls := 1;
      cbmxctrl := SizeOf(TMixerLine);
      pamxctrl := @mxc;
      pamxctrl^.cbStruct := SizeOf(TMixerControl);
    end;
    mixerGetLineControls(aMixer, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
    rc := mixerGetLineControls(aMixer, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
    Result := rc = MMSYSERR_NOERROR;
  end;
end;
function SetVolumeControl(aMixer: HMixer; mxc: TMixerControl; volume: Longint): Boolean;
var
  mxcd: TMixerControlDetails;
  vol: TMixerControlDetails_Unsigned;
  rc: MMRESULT;
begin
  FillChar(mxcd, SizeOf(mxcd), 0);
  with mxcd do
  begin
    cbStruct := SizeOf(TMixerControlDetails);
    dwControlID := mxc.dwControlID;
    cbDetails := SizeOf(TMixerControlDetails_Unsigned);
    paDetails := @vol;
    cMultipleItems := 0;
    cChannels := 1;
  end;
  vol.dwValue := volume;
  rc := mixerSetControlDetails(aMixer, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE);
  Result := rc = MMSYSERR_NOERROR;
end;
function InitMixer: HMixer;
var
  Err: MMRESULT;
begin
  Err := mixerOpen(@Result, 0, 0, 0, 0); 
  if Err <> MMSYSERR_NOERROR then
  Result := 0;
end;
// Example:
procedure SetMasterVolumeToZero;
var
  MyMixerHandle: HMixer;
  MyVolCtrl: TMixerControl;
begin
  MyMixerHandle := InitMixer;
  if MyMixerHandle <> 0 then
  try
    FillChar(MyVolCtrl, SizeOf(MyVolCtrl), 0);
    if GetVolumeControl(MyMixerHandle, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_VOLUME, MyVolCtrl) then
    begin
{The last parameter (0) here is the volume level}
      if SetVolumeControl(MyMixerHandle, MyVolCtrl, 0) then
      ShowMessage('Volume should now be set to zero');
    end;
  finally
    mixerClose(MyMixerHandle);
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetMasterVolumeToZero
end;

Pero lo que no he podido encontrar es la forma de obtener el valor del volumen general cuando lanzo mi aplicación (valor entre 0 y 65535).
¿Alguien sabe como averiguarlo?

Saludos...
Miguel Angel

cHackAll 22-10-2007 19:41:03

Cita:

Empezado por madiazg (Mensaje 239237)
...Pero lo que no he podido encontrar es la forma de obtener el valor del volumen general cuando lanzo mi aplicación (valor entre 0 y 65535)...

Código Delphi [-]
uses MMSystem; 
 
var 
 Handle, Volume: Cardinal; 
 Details: TMixerControlDetails = (cbStruct: SizeOf(TMixerControlDetails); 
                                  cChannels: 2; 
                                  cbDetails: SizeOf(Volume); 
                                  paDetails: @Volume); 
 
begin 
 mixerOpen(@Handle, 0, 0, 0, MIXER_OBJECTF_MIXER); 
 mixerGetControlDetails(Handle, @Details, MIXER_GETCONTROLDETAILSF_VALUE); 
 mixerClose(Handle); 
end.

0j0: Volume

madiazg 26-10-2007 19:47:00

Hola,
gracias por la aportación pero no me acaba de funcionar este código. Por favor, ¿puedes comprobarlo y poner un ejemplo de su utilización?
Gracias y saludos...
Miguel Angel

cHackAll 26-10-2007 23:04:42

Código Delphi [-]
...

implementation
 
{$R *.dfm}
 
uses MMSystem;
 
var
 Volume: Cardinal;
 Details: TMixerControlDetails = (cbStruct: SizeOf(TMixerControlDetails);
                                  cChannels: 2;
                                  cbDetails: SizeOf(Volume);
                                  paDetails: @Volume);
 
function GetVolume: LongBool;
var Handle: Cardinal;
begin
 mixerOpen(@Handle, 0, 0, 0, MIXER_OBJECTF_MIXER);
 Result := not LongBool(mixerGetControlDetails(Handle, @Details, MIXER_GETCONTROLDETAILSF_VALUE));
 if not (Result or LongBool(Details.dwControlID)) then
  begin
   Inc(Details.dwControlID);
   Result := GetVolume;
  end;
 mixerClose(Handle);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
 if GetVolume then
  Caption := IntToHex(Volume, 8);
end;
 
...
 
end.

0j0: No es la forma "correcta" pero es la mas sencilla.

madiazg 27-10-2007 13:56:17

Pues la verdad es que no se por qué no me funcionaba antes, pero ahora va estupendamente. Muchas gracias.
Saludos...
Miguel Angel.


La franja horaria es GMT +2. Ahora son las 12:32:34.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi