lusix1 2017-11-15 13:36 采纳率: 0%
浏览 1717
已结题

控制win7 混音器中的程序静音 求delphi 实现方案

unit MMDevApi;

interface

uses
Windows, ActiveX, ComObj;

const
CLASS_IMMDeviceEnumerator : TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
IID_IMMDeviceEnumerator : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
IID_IMMDevice : TGUID = '{D666063F-1587-4E43-81F1-B948E807363F}';
IID_IMMDeviceCollection : TGUID = '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}';
IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
IID_IAudioMeterInformation : TGUID = '{C02216F6-8C67-4B5B-9D00-D008E73E0064}';
IID_IAudioEndpointVolumeCallback : TGUID = '{657804FA-D6AD-4496-8A60-352752AF4F89}';
IID_IAudioSessionManager : TGUID = '{BFA971F1-4D5E-40BB-935E-967039BFBEE4}';
IID_IAudioSessionManager2 : TGUID = '{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}';

DEVICE_STATE_ACTIVE = $00000001;
DEVICE_STATE_UNPLUGGED = $00000002;
DEVICE_STATE_NOTPRESENT = $00000004;
DEVICE_STATEMASK_ALL = $00000007;

type
EDataFlow = TOleEnum;

const
eRender = $00000000;
eCapture = $00000001;
eAll = $00000002;
EDataFlow_enum_count = $00000003;

type
ERole = TOleEnum;

const
eConsole = $00000000;
eMultimedia = $00000001;
eCommunications = $00000002;
ERole_enum_count = $00000003;

type

IAudioSessionControl = interface(IUnknown)
['{F4B1A599-7266-4319-A8CA-E70ACB11E8CD}']
function GetDisplayName(out str:string ): Integer; stdcall; //可能有错
end;

IAudioSessionControl2 = interface(IAudioSessionControl)
['{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}']
function GetProcessId(out pRetVal: integer): Integer; stdcall;
function GetSession(const SessionCount: integer; out Session: IAudioSessionControl): Integer; stdcall;
end;

IAudioSessionEnumerator = interface(IUnknown)
['{E2F5BB11-0570-40CA-ACDD-3AA01277DEE8}']
function GetCount(out SessionCount: integer): Integer; stdcall;
function GetSession(const SessionCount: integer; out Session: IAudioSessionControl): Integer; stdcall;
end;
IAudioSessionNotification = interface(IUnknown)
['{641DD20B-4D41-49CC-ABA3-174B9477BB08}']
end;

ISimpleAudioVolume = interface(IUnknown)
['{87CE5498-68D6-44E5-9215-6DA47EF883D8}']
function SetMute(bMute: Boolean; pguidEventContext: PGUID): Integer; stdcall;
function GetMute(out bMute: Boolean): Integer; stdcall;
function SetMasterVolume(fLevelDB: single; pguidEventContext: PGUID): Integer; stdcall;
function GetMasterVolume(out fLevelDB: single): Integer; stdcall;
end;
IAudioSessionManager = interface(IUnknown)
['{BFA971F1-4D5E-40BB-935E-967039BFBEE4}']
function GetSessionEnumerator(out SessionEnum: IAudioSessionEnumerator): HResult; stdcall;
end;
type
IAudioSessionManager2 = interface(IAudioSessionManager)
['{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}']
function GetSessionEnumerator(out SessionEnum: IAudioSessionEnumerator): HResult; stdcall;
// function RegisterSessionNotification(SessionNotification: IAudioSessionNotification): HResult; stdcall;
// function UnregisterSessionNotification(SessionNotification: IAudioSessionNotification): HResult; stdcall;
// function RegisterDuckNotification(const sessionID: LPCWSTR; const duckNotification: IAudioVolumeDuckNotification): HResult; stdcall;
// function UnregisterDuckNotification(const duckNotification: IAudioVolumeDuckNotification): HResult; stdcall;
end;

//==================================
IAudioEndpointVolumeCallback = interface(IUnknown)
['{657804FA-D6AD-4496-8A60-352752AF4F89}']
end;
IAudioEndpointVolume = interface(IUnknown)
['{5CDF2C82-841E-4546-9722-0CF74078229A}']
function RegisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;
function UnregisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;
function GetChannelCount(out PInteger): Integer; stdcall;
function SetMasterVolumeLevel(fLevelDB: single; pguidEventContext: PGUID): Integer; stdcall;
function SetMasterVolumeLevelScalar(fLevelDB: single; pguidEventContext: PGUID): Integer; stdcall;
function GetMasterVolumeLevel(out fLevelDB: single): Integer; stdcall;
function GetMasterVolumeLevelScaler(out fLevelDB: single): Integer; stdcall;
function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): Integer; stdcall;
function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): Integer; stdcall;
function GetChannelVolumeLevel(nChannel: Integer; out fLevelDB: double): Integer; stdcall;
function GetChannelVolumeLevelScalar(nChannel: Integer; out fLevel: double): Integer; stdcall;
function SetMute(bMute: Boolean; pguidEventContext: PGUID): Integer; stdcall;
function GetMute(out bMute: Boolean): Integer; stdcall;
function GetVolumeStepInfo(pnStep: Integer; out pnStepCount: Integer): Integer; stdcall;
function VolumeStepUp(pguidEventContext: PGUID): Integer; stdcall;
function VolumeStepDown(pguidEventContext: PGUID): Integer; stdcall;
function QueryHardwareSupport(out pdwHardwareSupportMask): Integer; stdcall;
function GetVolumeRange(out pflVolumeMindB: double; out pflVolumeMaxdB: double; out pflVolumeIncrementdB: double): Integer; 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 pSessionManager:IAudioSessionManager2): Integer; stdCall;
// 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: EDataFlow; 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;
//======================================

implementation

end.

//==========================================

procedure TForm2.btntestClick(Sender: TObject);
var
deviceEnumerator: IMMDeviceEnumerator;
defaultDevice: IMMDevice;
VolumeLevel: Single;
EndpointVolume: IAudioEndpointVolume;
sess:TObject ;
Session:IAudioSessionManager2;
sessionEnumerator:IAudioSessionEnumerator ;
pinter:pointer;
i,int1:Integer;
ctl:IAudioSessionControl;
str:string;
begin

CoInitialize( nil );

// CoCreateInstance( CLSID_MMDeviceEnumerator, nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, DeviceEnumerator );
// DeviceEnumerator.GetDefaultAudioEndpoint( eRender, eMultimedia, DefaultDevice );
CoCreateInstance(CLASS_IMMDeviceEnumerator, nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, deviceEnumerator);
deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, defaultDevice);
Memo1.Lines.Add('defaultDevice = ');
// defaultDevice.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, endpointVolume);
if defaultDevice.Activate(IID_IAudioSessionManager2, 0, nil, Session )=S_OK then
begin
Memo1.Lines.Add('Activate IID_IAudioSessionManager2 = ok ');
end else
begin
Memo1.Lines.Add('Activate IID_IAudioSessionManager2 = 出错 ');
Exit;
end;

Session.GetSessionEnumerator(sessionEnumerator) ;

// 上面这句出错,应是找不到这个函数接口

 Memo1.Lines.Add('GetSessionEnumerator');

sessionEnumerator.GetCount(int1);
for i:=1 to int1-1 do
begin
sessionEnumerator.GetSession(i,ctl);
ctl.GetDisplayName(str);
Memo1.Lines.Add(str);
end;

VolumeLevel := 0;
endpointVolume.SetMute(True,nil);
// endpointVolume.SetMasterVolumeLevelScalar(VolumeLevel, nil);
// Caption := Format('%1.8f', [VolumeLevel])
end ;

///////////////////////////////////////////////
Session.GetSessionEnumerator(sessionEnumerator) ;

                // 上面这句出错,应是找不到这个函数接口

求delphi 实现方案,完整的音量控制单元,并能控制混音器中程序的音量

参考c资料:

1,http://bbs.csdn.net/topics/390021167

2,http://bbs.csdn.net/topics/370044766

3, http://blog.csdn.net/yzxun/article/details/38519507?locationNum=1&fps=1

  • 写回答

1条回答

  • gooodhelp 2018-06-18 15:23
    关注

    你的代码有错,根据C语言的翻译如下
    procedure TForm1.btn4Click(Sender: TObject);
    var
    deviceEnumerator: IMMDeviceEnumerator;
    defaultDevice: IMMDevice;
    VolumeLevel: Single;
    EndpointVolume: IAudioEndpointVolume;
    sess:TObject ;
    Session:IAudioSessionManager2;
    sessionEnumerator:IAudioSessionEnumerator ;
    pinter:pointer;
    i,int1,hr:Integer;
    session_ctrl1:IAudioSessionControl;
    session_ctrl2:IAudioSessionControl2;
    str:PWideChar;
    myguid:TGUID;
    pid:DWORD;
    simple_vol:ISimpleAudioVolume;
    begin
    mmo1.Clear;
    hr:=CoCreateInstance(CLSID_MMDeviceEnumerator, nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, deviceEnumerator);
    hr:=deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, defaultDevice);
    //Mmo1.Lines.Add('defaultDevice = '+inttostr(hr));
    // defaultDevice.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, endpointVolume);
    if defaultDevice.Activate(IID_IAudioSessionManager2, CLSCTX_INPROC_SERVER, nil, Session )=S_OK then
    begin
    //Mmo1.Lines.Add('Activate IID_IAudioSessionManager2 = ok ');
    end else
    begin
    //Mmo1.Lines.Add('Activate IID_IAudioSessionManager2 = 出错 ');
    Exit;
    end;

    hr:=Session.GetSessionEnumerator(sessionEnumerator) ;
    hr:=sessionEnumerator.GetCount(int1);
    for i:=1 to int1-1 do//从1开始,如果从 0开始就包括了系统声音
    begin
    hr:=sessionEnumerator.GetSession(i,session_ctrl1);
    if not Succeeded(hr) then Continue;
    session_ctrl1.GetDisplayName(str);
    Mmo1.Lines.Add(str);
    session_ctrl1.QueryInterface(IID_IAudioSessionControl2,session_ctrl2);
    hr:=session_ctrl2.GetProcessId(pid); //进程ID
    mmo1.Lines.Add(IntToStr(pid));
    session_ctrl2.QueryInterface(IID_ISimpleAudioVolume,simple_vol);
    VolumeLevel:=0.5;
    simple_vol.SetMasterVolume(VolumeLevel,nil); //指定进程ID的程序的音量 为0.5
    end;
    end;

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题