PDA

Ver la Versión Completa : Volumen de mi PC


madiazg
17-10-2007, 19:22:17
Hola, buenos días,
mediante este código puedo controlar el nivel del volumen:
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, 18:41:03
...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)...

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, 18: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, 22:04:42
...

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, 12:56:17
Pues la verdad es que no se por qué no me funcionaba antes, pero ahora va estupendamente. Muchas gracias.
Saludos...
Miguel Angel.