unit MMDevApi;interfaceuses
  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}';
  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
  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 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;implementationend.
在国外网站找到以上代码,是个WIN7 WIN8的声音控制单元。但是无奈小弟英文太烂,中文这方面资料还太少。求教了。
var
  Form1: TForm1;
  EndpointVolume: IAudioEndpointVolume = nil;implementation{$R *.dfm}procedure TForm1.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;procedure TForm1.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;上边这段可以修改系统声音大小,但是不知道怎么修改MIC声音大小。

解决方案 »

  1.   


    XP 没有WIN8的启动速度啊~呵呵。
    还有就是不合适触摸 我也喜欢XP 不得已啊。
      

  2.   

    Windows VISTA、Windows server 2008等系统音频系统底层API-转  2010-01-07 19:13:40|  分类: 默认分类 |字号 订阅
    Core Audio APIs的优势: Windows Vista、Windows 7、Windows server 2008等系统音频系统相比之前的系统有很大的变化,产生了一套新的底层API即Core Audio APIs。该低层API为高层API( 如Media Foundation(将要取代DirectShow等高层API)等 )提供服务。相比之前版本的API有如下优势: 1. 具有低延时、故障恢复能力的音频流。2. 提高可靠性 ( 将很多音频函数从核心态移到了用户态 )3. 提高了安全性 (在安全的,低优先级别的线程处理被保护的音频内容)4. 为单独的音频设备分配了特定的系统级别的规则 (console, multimedia, communications) 。5. 用户可以直接操作相应音频终端设备(Audio Endpoint Devices 如:扬声器、耳机、麦克风、CD播放器)的软件抽象。 Core Audio APIs在系统中的位置: Core Audio APIs的组成: 1. Multimedia Device (MMDevice) API    该API用于枚举系统中的音频终端设备(Audio Endpoint Devices)。告诉音频客户端程序有哪些音频终端设备以及它们的性能,并且为这些设备创建驱动实例(driver instances)。是最基本的Core Audio API,为其他三个API提供服务。主要接口:IMMDeviceEnumerator 用来列举音频终端设备。IMMDevice                  代表一个音频设备(audio device)。IMMEndpoint               代表一个音频终端设备(audio endpoint device),只有一个方法GetDataFlow,用来识别一个音频终端设备是一个输出设备(rendering device)还是一个输入设备(capture device)。IMMDeviceCollection           代表一个音频终端设备的集合 2. EndpointVolume API      使客户端程序能够操作音频终端设备。主要接口:IAudioEndpointVolume 用于控制音频终端设备的音量、静音。 3. Windows Audio Session API (WASAPI)      使客户端程序能够管理介于程序和音频终端设备之间的音频数据。                                                        如应用程序本身的音量。主要接口:ISimpleAudioVolume           代表控制来往音频终端设备的音频流的音量。  4. DeviceTopology API 客户端程序使用这个API可直接沿着音频适配器(audio adapters)的硬件设备里的数据通道进入布局特征。是最底层的音频API,可以通过适配器设备(adapter devices)的布局来查看和管理设备中的音频控制。下图为DeviceTopology API的作用范围  音频适配器设备(audio adapter device)内部是由很多Part组成。主要包括亚单位(Subunit)和连接头(Connector)。亚单位主要分为:音量控制(Vol)、静音控制(Mute)、多路器(MUX)等。连接头(Con)是一个连接的两端。 Core Audio APIs的使用:<1> 音频终端设备(Audio Endpoint Device)音量、静音控制比如控制“声音”对话框下“播放”选项卡中的“扬声器”等,“录制”选项卡中的“麦克风”、“立体声混音”等。步骤:1.        得到IMMDeviceEnumerator接口。2.        遍历音频终端设备得到IMMDeviceCollection接口。3.        根据IMMDeviceCollection得到每个设备的IPropertyStore接口,从而分辨并搜索到指定的设备IMMDevice接口。4.        用IMMDevice接口激活音量、静音控制接口IAudioEndpointVolume。 <2> 音频适配器设备亚单位(Subunit)的音量、静音控制         比如控制“声音”对话框下“播放”选项卡中的“扬声器”下的“CD 音频”、“麦克风”、“Fornt Pink In”等的控制。步骤:1.        得到IMMDeviceEnumerator接口。2.        遍历音频终端设备得到IMMDeviceCollection接口。3.        根据IMMDeviceCollection得到每个设备的IPropertyStore接口,从而分辨并搜索到指定的设备IMMDevice接口。4.        根据IMMDevice接口和连接器接口IConnector得到音频终端设备所对应的设备适配器(adapter device)的布局接口IDeviceTopology。5.        根据IDeviceTopology接口得到指定的部分(Part)。6.        最后用得到的部分(Part)接口IPart激活音量控制接口IAudioVolumeLevel和静音控制接口IAudioMute。 <3> 会话(Session)音量、静音控制         比如“音量合成器”中应用程序的音量。 步骤:1.        得到IMMDeviceEnumerator接口。2.        根据IMMDeviceEnumerator得到默认设备的IMMDevice接口。3.        得到音频会话接口IAudioSessionManager。4.        由IAudioSessionManager接口得到IAudioSessionControl接口,该接口用来设置会话(Session)参数。5.        设置好了参数之后,在由IAudioSessionManager接口得到音频会话的音量、静音控制接口ISimpleAudioVolume,从而可以控制程序的音量、静音。 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhaozy694992769/archive/2009/11/26/4876295.aspx
      

  3.   

    完了 看来DELPHI真的没落了!
      

  4.   

    没做过录音相关的工作,根据本人了解:
    WASAPI的endpoint分为几种类型eCapture,eRender,eAll
    其中eRender相当于回放
    eCapture应该是录音 deviceEnumerator.GetDefaultAudioEndpoint(eRender, eConsole, defaultDevice);
    这个是获得到的回放设备,所以调节的音量是音箱的,把eRender换成eCapture试试
      

  5.   

    根据erole的定义,可能econsole也要修改
    eConsole    Games, system notification sounds, and voice commands.
    eMultimedia    Music, movies, narration, and live music recording.
    eCommunications    Voice communications (talking to another person).
    ERole_enum_count    The number of members in the ERole enumeration (not counting the ERole_enum_count member).