2013-07-12 21 views
3

我寫了一個非常簡單的應用程序,用於在Windows 7上運行的Delphi 2007中播放我的音樂集。我爲了避免使用Delphi Media玩家組件,它拒絕播放一些我放在它上面的曲目。現在我想從應用程序中控制揚聲器的音量。控制Windows 7中的主音箱音量

在這一點上我說「揚聲器的音量」而不是「WMP量」,因爲我想控制WMP的體積比控制整個揚聲器/主音量的所有應用程序更加困難。

我敢肯定,這在過去是很簡單,但操作系統變得更加複雜,而我發現並試圖不會對主設備/核心音量控制揚聲器工作的所有代碼片段。

是否有可能從在Windows 7上運行的Delphi 2007控制這個主卷?

如果是,那麼一對夫婦像GetVolume和SetVolume功能我可以用它來做些什麼這樣的基本任務將是非常讚賞。

我已經看過過去問過的類似問題,但我並不樂觀。

布魯斯。

+0

德爾福版本在問題@Tlama中被提及兩次,但我沒有看到它是如此相關以至於它需要包含在標記中。你認爲不同的版本會要求不同的答案嗎? –

+0

@搖滾啊,對不起。收回。我在問,因爲類型庫導入可能存在差異...(即使不應該,我知道)。但是沒關係,我現在沒有時間瞭解這個問題了。 – TLama

+0

是否可以與[在Windows Vista中設置音量]相同(http://blogs.msdn.com/b/larryosterman/archive/2007/03/06/how-do-i-change-the-master-容積式窗口,vista.aspx)? –

回答

5

在Windows 7下,您必須使用Core Audio SDK來控制Windows音頻。從這裏您可以嘗試IAudioEndpointVolume::SetMasterVolumeLevelScalarIAudioEndpointVolume::SetMasterVolumeLevel方法。

試試這個使用SetMasterVolumeLevelScalar方法的代碼。

{$APPTYPE CONSOLE} 

uses 
    SysUtils, 
    Windows, 
    ActiveX, 
    ComObj; 

const 
    CLASS_IMMDeviceEnumerator : TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}'; 
    IID_IMMDeviceEnumerator : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}'; 
    IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}'; 

type 
    IAudioEndpointVolumeCallback = interface(IUnknown) 
    ['{657804FA-D6AD-4496-8A60-352752AF4F89}'] 
    end; 

    IAudioEndpointVolume = interface(IUnknown) 
    ['{5CDF2C82-841E-4546-9722-0CF74078229A}'] 
    function RegisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; stdcall; 
    function UnregisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; stdcall; 
    function GetChannelCount(out PInteger): HRESULT; stdcall; 
    function SetMasterVolumeLevel(fLevelDB: single; pguidEventContext: PGUID): HRESULT; stdcall; 
    function SetMasterVolumeLevelScalar(fLevelDB: single; pguidEventContext: PGUID): HRESULT; stdcall; 
    function GetMasterVolumeLevel(out fLevelDB: single): HRESULT; stdcall; 
    function GetMasterVolumeLevelScaler(out fLevelDB: single): HRESULT; stdcall; 
    function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): HRESULT; stdcall; 
    function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): HRESULT; stdcall; 
    function GetChannelVolumeLevel(nChannel: Integer; out fLevelDB: double): HRESULT; stdcall; 
    function GetChannelVolumeLevelScalar(nChannel: Integer; out fLevel: double): HRESULT; stdcall; 
    function SetMute(bMute: Boolean; pguidEventContext: PGUID): HRESULT; stdcall; 
    function GetMute(out bMute: Boolean): HRESULT; stdcall; 
    function GetVolumeStepInfo(pnStep: Integer; out pnStepCount: Integer): HRESULT; stdcall; 
    function VolumeStepUp(pguidEventContext: PGUID): HRESULT; stdcall; 
    function VolumeStepDown(pguidEventContext: PGUID): HRESULT; stdcall; 
    function QueryHardwareSupport(out pdwHardwareSupportMask): HRESULT; stdcall; 
    function GetVolumeRange(out pflVolumeMindB: double; out pflVolumeMaxdB: double; out pflVolumeIncrementdB: double): HRESULT; stdcall; 
    end; 

    IAudioMeterInformation = interface(IUnknown) 
    ['{C02216F6-8C67-4B5B-9D00-D008E73E0064}'] 
    end; 

    IPropertyStore = interface(IUnknown) 
    end; 

    IMMDevice = interface(IUnknown) 
    ['{D666063F-1587-4E43-81F1-B948E807363F}'] 
    function Activate(const refId: TGUID; dwClsCtx: DWORD; pActivationParams: PInteger; out pEndpointVolume: IAudioEndpointVolume): HRESULT; stdCall; 
    function OpenPropertyStore(stgmAccess: DWORD; out ppProperties: IPropertyStore): HRESULT; stdcall; 
    function GetId(out ppstrId: PLPWSTR): HRESULT; stdcall; 
    function GetState(out State: Integer): HRESULT; stdcall; 
    end; 


    IMMDeviceCollection = interface(IUnknown) 
    ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}'] 
    end; 

    IMMNotificationClient = interface(IUnknown) 
    ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}'] 
    end; 

    IMMDeviceEnumerator = interface(IUnknown) 
    ['{A95664D2-9614-4F35-A746-DE8DB63617E6}'] 
    function EnumAudioEndpoints(dataFlow: TOleEnum; deviceState: SYSUINT; DevCollection: IMMDeviceCollection): HRESULT; stdcall; 
    function GetDefaultAudioEndpoint(EDF: SYSUINT; ER: SYSUINT; out Dev :IMMDevice): HRESULT; stdcall; 
    function GetDevice(pwstrId: pointer; out Dev: IMMDevice): HRESULT; stdcall; 
    function RegisterEndpointNotificationCallback(pClient: IMMNotificationClient): HRESULT; stdcall; 
    end; 

procedure SetMasterVolume(fLevelDB: single); 
var 
    pEndpointVolume: IAudioEndpointVolume; 
    LDeviceEnumerator: IMMDeviceEnumerator; 
    Dev: IMMDevice; 
begin 
    if not Succeeded(CoCreateInstance(CLASS_IMMDeviceEnumerator, nil, CLSCTX_INPROC_SERVER, IID_IMMDeviceEnumerator, LDeviceEnumerator)) then 
    RaiseLastOSError; 
    if not Succeeded(LDeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Dev)) then 
    RaiseLastOSError; 

    if not Succeeded(Dev.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, pEndpointVolume)) then 
    RaiseLastOSError; 

    if not Succeeded(pEndpointVolume.SetMasterVolumeLevelScalar(fLevelDB, nil)) then 
    RaiseLastOSError; 
end; 


begin 
try 
    CoInitialize(nil); 
    try 
     SetMasterVolume(1.0); //set the volume to the max 
     Sleep(2000); 
     SetMasterVolume(0.5); //set the volume to the 50 % 
     Sleep(2000); 
     SetMasterVolume(0.1); //set the volume to the 10 % 
     Sleep(2000); 
    finally 
     CoUninitialize; 
    end; 
except 
    on E:Exception do 
     Writeln(E.Classname, ':', E.Message); 
end; 
Writeln('Press Enter to exit'); 
Readln; 
end.