我想以编程方式调整音量,例如vista和xp中的Get / SetMasterVolume?使用mmsystem单元?
答案 0 :(得分:3)
以下是音频通用api的实现:MMDevApi
以及带按钮的示例
unit Unit33;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MMDevApi, ActiveX, StdCtrls;
type
TForm33 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form33: TForm33;
endpointVolume: IAudioEndpointVolume = nil;
implementation
{$R *.dfm}
procedure TForm33.Button1Click(Sender: TObject);
var
VolumeLevel: Single;
begin
if endpointVolume = nil then Exit;
VolumeLevel := 0.50;
endpointVolume.SetMasterVolumeLevelScalar(VolumeLevel, nil);
Caption := Format('%1.8f', [VolumeLevel])
end;
procedure TForm33.FormCreate(Sender: TObject);
var
deviceEnumerator: IMMDeviceEnumerator;
defaultDevice: IMMDevice;
begin
CoCreateInstance(CLASS_IMMDeviceEnumerator, nil, CLSCTX_INPROC_SERVER, IID_IMMDeviceEnumerator, deviceEnumerator);
deviceEnumerator.GetDefaultAudioEndpoint(eRender, eConsole, defaultDevice);
defaultDevice.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, endpointVolume);
end;
end.
答案 1 :(得分:2)
Windows XP:
function SetMasterVolume(VolToSet: word; out VolSet: word): MMResult;
var
MixerHandle: HMixer;
Volume: TMixerControlDetails_Unsigned;
MixerLine: TMixerLine;
MixerLineControls: TMixerLineControls;
VolumeCtrl: TMixerControl;
MixerControlDetails: TMixerControlDetails;
begin
// Get mixer handle
Result := mixerOpen(@MixerHandle, 0, 0, 0, 0);
if Result <> MMSYSERR_NOERROR then Exit;
try
// Get master volume line
FillChar(MixerLine, SizeOf(TMixerLine), 0);
MixerLine.cbStruct := SizeOf(TMixerLine);
MixerLine.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
Result := mixerGetLineInfo(MixerHandle, @MixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE);
if Result <> MMSYSERR_NOERROR then Exit;
// Get the volume control of the master volume line
FillChar(VolumeCtrl, SizeOf(TMixerControl), 0);
MixerLineControls.cbStruct := SizeOf(TMixerLineControls);
MixerLineControls.dwLineID := MixerLine.dwLineID;
MixerLineControls.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
MixerLineControls.cControls := 1;
MixerLineControls.cbmxctrl := SizeOf(TMixerControl);
MixerLineControls.pamxctrl := @VolumeCtrl;
Result := mixerGetLineControls(MixerHandle,@MixerLineControls,MIXER_GETLINECONTROLSF_ONEBYTYPE);
if Result <> MMSYSERR_NOERROR then Exit;
// Set details (volume) for the volume control of the master volume line
FillChar(MixerControlDetails, SizeOf(TMixerControlDetails), 0);
MixerControlDetails.cbStruct := SizeOf(TMixerControlDetails);
MixerControlDetails.dwControlID := VolumeCtrl.dwControlID;
MixerControlDetails.cChannels := 1;
MixerControlDetails.cMultipleItems := 0;
MixerControlDetails.cbDetails := SizeOf(TMixerControlDetails_Unsigned);
MixerControlDetails.paDetails := @Volume;
Volume.dwValue := VolToSet;
Result := mixerSetControlDetails(MixerHandle, @MixerControlDetails,MIXER_SETCONTROLDETAILSF_VALUE);
finally
mixerClose(MixerHandle);
end;
end;
电话:
var y:word;
begin
SetMasterVolume(2000,y);
end;
答案 2 :(得分:0)
MMDevApi有许多不正确的声明。双参数应该是单一的。布尔参数更好地作为整数。并且许多(如果不是全部)TGUID参数应该是PGUID。在更正声明之后,我能够设置静音和音量级别。