|
来自: http://social.msdn.microsoft.com ... 9-a8c9-2e27eb9ec058
?% \1 R" O9 N+ t' fBy Mukta & ToxicDream- ) a9 `$ E1 ], ?
- //for delphi 59 h/ T W( P) t) v4 n
- unit MMDevApi_tlb;0 `/ O) S! _9 F1 J& o9 E
8 T; m5 l/ q. }/ c6 _0 G5 _7 g$ ]- c, d- interface
$ P9 f9 W1 L/ Z5 p( }+ ] - uses Windows, ActiveX, Classes, Graphics, OleServer, OleCtrls, StdVCL,ComObj;
! J+ J a$ }' B - const# o V3 G/ X' }2 @2 s
- // TypeLibrary Major and minor versions
7 A6 L+ f* x5 w3 j0 O3 ~ - ! x2 r8 Y; }: g+ ]9 D: B9 i% H
- CLASS_IMMDeviceEnumerator: TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
; {# [! G+ P+ O/ Q - IID_IMMDeviceEnumerator: TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';; O8 c) c) o3 O5 U) A* B6 i
- IID_IMMDevice: TGUID = '{D666063F-1587-4E43-81F1-B948E807363F}';
7 N) R5 x# Q% L- T9 B2 ] - IID_IMMDeviceCollection: TGUID = '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}';7 I) j' ?6 D$ X
- IID_IAudioEndpointVolume: TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
! m" O1 v" ~5 F. X- @/ R! V; E - IID_IAudioMeterInformation : TGUID = '{C02216F6-8C67-4B5B-9D00-D008E73E0064}';
/ x0 M1 W* S6 Y+ R/ f - IID_IAudioEndpointVolumeCallback: TGUID = '{657804FA-D6AD-4496-8A60-352752AF4F89}';
5 {3 g+ @# K& A) I, E: t9 l$ v. C - - [$ P, D# m: G1 W4 C1 W# D" K% ^! X4 n
- DEVICE_STATE_ACTIVE = $00000001;
# P& D; r- ], A% \5 T - DEVICE_STATE_UNPLUGGED = $00000002;* _( ~4 W G8 ~
- DEVICE_STATE_NOTPRESENT = $00000004;, ^3 \- S" k; H6 V; M; Y' ]
- DEVICE_STATEMASK_ALL = $00000007;
* Y/ r8 {6 N+ {4 z$ {
: ~+ u* u- X& ^! s5 d* M- type* }/ u+ X, i) `7 r1 M
- EDataFlow = TOleEnum;
5 a) {6 T" A* |7 h - const8 [2 u d7 U) l# o. M
- eRender = $00000000;
; i# V$ O3 c/ K" n5 \4 ] - eCapture = $00000001;! A+ W3 Q' {- r; K0 i2 q+ h% E& a
- eAll = $00000002;
/ C" R! v# [* D - EDataFlow_enum_count = $00000003;
6 k9 Z9 w, w$ Z k
# d% U; T4 z) E- type
4 r" v7 R$ N% C, Z - ERole = TOleEnum;: o/ p# ?( q, q
- const
" Q1 o# a3 ~ X; [- ` - eConsole = $00000000;
6 h! A: K* _3 k - eMultimedia = $00000001;# `7 Y- h& `: X
- eCommunications = $00000002;8 D/ f, o# A- E [+ S
- ERole_enum_count = $00000003;% |# D; T+ q1 {: n
' D* z1 l- ~ @9 o- type' r7 S+ `* D! o; ]: j6 j3 _
- IAudioEndpointVolumeCallback = interface(IUnknown)( f% q/ n2 T' A" [0 J) ~
- ['{657804FA-D6AD-4496-8A60-352752AF4F89}']# N O) Q" M# S: R
- end;8 T5 B' d5 c8 O$ }" L/ n) ?
- + P# l1 Q2 |* p7 T
- IMMAudioEndpointVolume = interface(IUnknown)7 F& E; X" B/ X, a* c" H
- ['{5CDF2C82-841E-4546-9722-0CF74078229A}']
p: P% }2 M) y: | - Function RegisterControlChangeNotify( AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;4 ?* N* x- F* u
- Function UnregisterControlChangeNotify( AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;/ Q o' n, K( A# }8 L$ P! x H
- Function GetChannelCount(out PInteger): Integer; stdcall;
' s* n& u. |/ L9 V8 j - Function SetMasterVolumeLevel(fLevelDB: double; pguidEventContext: TGUID):Integer; stdcall;" w0 _% i1 ~( t* M- J$ e
- Function SetMasterVolumeLevelScalar(fLevelDB: double; pguidEventContext: TGUID):Integer; stdcall;
( K5 ~+ r. B) E) |7 h+ T r) W9 X$ p - Function GetMasterVolumeLevel(out fLevelDB: double):Integer; stdcall;$ a& G! n |7 f4 H- N
- Function GetMasterVolumeLevelScaler(out fLevel: double):Integer; stdcall;
9 D4 l4 M; r# E& m - Function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: double; pguidEventContext: TGUID):Integer; stdcall;
, x3 Z1 M& m8 [% {7 ~ n - Function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: double; pguidEventContext: TGUID):Integer; stdcall;
$ \* q6 X0 q9 s - Function GetChannelVolumeLevel(nChannel: Integer; out fLevelDB: double) : Integer; stdcall;) h( \* Z6 g/ J I# D0 h" M; M
- Function GetChannelVolumeLevelScalar(nChannel: Integer; out fLevel: double) : Integer; stdcall;
3 y7 F2 i# H- f& F: F* l! z - Function SetMute(bMute: Boolean ; pguidEventContext: TGUID) :Integer; stdcall;
3 K: I& p; P* ^ K0 Y) F# ~: ^2 i - Function GetMute(out bMute: Boolean ) :Integer; stdcall;
$ Z8 q$ M6 G% z$ E; J( i! Z% I - Function GetVolumeStepInfo( pnStep: Integer; out pnStepCount: Integer):Integer; stdcall;
' U- h& I1 s" P O, o. |; s - Function VolumeStepUp(pguidEventContext: TGUID) :Integer; stdcall;7 K5 j* K( T7 i) N: b! \( k$ q
- Function VolumeStepDown(pguidEventContext: TGUID) :Integer; stdcall;- ]1 N' u2 d$ K' u/ E5 F
- Function QueryHardwareSupport(out pdwHardwareSupportMask): Integer; stdcall;
% f9 ^+ V6 z9 @7 v4 N8 [3 z2 A7 d - Function GetVolumeRange(out pflVolumeMindB: double; out pflVolumeMaxdB: double; out pflVolumeIncrementdB: double): Integer; stdcall;
3 M4 a$ c) H4 |( G0 K2 U0 H - end;" {- P3 I, f1 o t5 G, K, q( m
0 X. b, |3 I5 W$ b0 [- b% x6 ?- { IAudioMeterInformation = interface(IUnknown)
7 m- V; l2 E) ?0 M3 X0 O$ l/ g. s - ['{C02216F6-8C67-4B5B-9D00-D008E73E0064']
0 R( c- L) V8 Z2 {) u4 \8 [ - end;}4 r) Q* a1 Z4 y) w. ?3 U9 U
- % i( @5 F; C( c3 @! i
- IPropertyStore = interface(IUnknown) l: e8 t9 A, [
- end;
' [2 k% x: P# ?2 y3 a+ N - . D' i3 \0 Y6 _/ k3 S
- type
. r9 h+ F T2 T - IMMDevice = interface(IUnknown)
% B: x. } G0 s; ~! f: R - ['{D666063F-1587-4E43-81F1-B948E807363F}']4 W- o% C1 H$ A5 s
- Function Activate( refId :TGUID;
~# `1 P" q+ b/ m. r4 C O - dwClsCtx: DWORD;
/ y! P ^. b" r2 ^# B8 s - pActivationParams: PInteger ; ~" @7 m, F8 }2 h4 b
- out pEndpointVolume: IMMAudioEndpointVolume): Hresult; stdCall;
O! w* \. B, Z( n- r) ` - Function OpenPropertyStore(stgmAccess: DWORD; out ppProperties :IPropertyStore): Hresult; stdcall;
! g6 x8 m( ^3 X, @1 } - Function GetId(out ppstrId: PLPWSTR ): Hresult; stdcall;
8 n9 m0 m" W: k( ~- o - Function GetState(out State :Integer): Hresult; stdcall;
1 w1 K$ {9 d8 U( N% |) i
' C# O. \+ K: b- end;
9 A7 J. [( d* d- f% R. e
- D) l/ i% J. D2 J- # A, W B% }3 A. f1 ]- |+ i2 e Y
- IMMDeviceCollection = interface(IUnknown)
o4 U# w- p% u5 ] - ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']6 h+ B) W1 w9 E; l$ J
- end;
) t( F4 k# X& \7 o2 [4 Z - % g: r& o8 n5 @
- IMMNotificationClient = interface (IUnknown)4 b/ H+ G; u0 q/ {0 |
- ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']" {( h p m" t0 c
- end;
+ J- U* U; `8 L' ~9 A
' I! r& |2 B/ p- t9 V- IMMDeviceEnumerator = interface(IUnknown)
% I# W- r: d. A. r- L6 l* H; K - ['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
. {: t5 e# d* d1 j" T; j( M, l% { - Function EnumAudioEndpoints( dataFlow: EDataFlow; deviceState: SYSUINT; DevCollection:IMMDeviceCollection ): Hresult ; stdcall;2 P/ m+ t3 I9 J9 |
- Function GetDefaultAudioEndpoint(EDF: SYSUINT; ER: SYSUINT; out Dev :IMMDevice ): Hresult ; stdcall;1 Y) n- P7 L3 b; O# }, V
- Function GetDevice( pwstrId: pointer ; out Dev :IMMDevice) : HResult; stdcall;
% `: L9 L: z' r0 w2 g - Function RegisterEndpointNotificationCallback(pClient :IMMNotificationClient) :Hresult; stdcall;
- y" U# v3 H* g! L, N - end;
5 W. f/ B( P* w u) L
' Z+ r, R/ L3 D8 h! ^- implementation7 B" n0 ?* e: P* J
- end.: c( o9 h. i$ D$ q7 D9 k
- 7 t3 _" e; {$ R+ o2 |8 x
- Thanks & Reagrds,
0 B4 L; O/ \# Z/ \" }1 b9 ~ - Mukta ...
6 f! a! }8 b+ y: P2 y9 S; F/ x
复制代码
2 z9 P! Y! ~# t k- it's work in Delphi 75 I7 \- Y |* N$ |8 O* E3 j6 \
- //////////////////////////
- X& s$ W9 H) c) R! F% ? - / o& w/ B% E( E/ j0 o. x
- unit MMDevApi;1 }8 v' ?& S# _+ l4 W. l7 {" i
$ ~0 {! b, s3 ^ \8 U" W/ M# e! `- interface* B4 _3 {& D, u6 M/ C
- 8 c4 w. j6 j8 c: w! Q, S; C7 x! d
- uses
6 p% \* n) N8 p' D* g8 v. `. z! o - Windows, ActiveX, ComObj;5 i* ]. ~5 T: I; J& Y p' t F
$ d5 u% [2 u' J/ I) O5 {8 f3 k- const. P; Q' j' r7 y+ w3 ~8 ~3 @6 J( J
- CLASS_IMMDeviceEnumerator : TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
+ n# B. Q5 h, q" s/ u - IID_IMMDeviceEnumerator : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
$ m- u& M' c0 r - IID_IMMDevice : TGUID = '{D666063F-1587-4E43-81F1-B948E807363F}';& ^2 v0 Z S, X
- IID_IMMDeviceCollection : TGUID = '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}';
( {1 H% j. u$ v% `6 N1 A: ?! } - IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';; H" I: H$ T. [: a
- IID_IAudioMeterInformation : TGUID = '{C02216F6-8C67-4B5B-9D00-D008E73E0064}';
0 z9 T% |8 `( I6 k - IID_IAudioEndpointVolumeCallback : TGUID = '{657804FA-D6AD-4496-8A60-352752AF4F89}';
8 P0 g. V: u. b. R
7 q/ V! ]4 s, G. [5 \- Y; D% e- DEVICE_STATE_ACTIVE = $00000001;
0 X d+ t) ^" M - DEVICE_STATE_UNPLUGGED = $00000002;
0 }% a' Z1 s( m6 N4 x; } - DEVICE_STATE_NOTPRESENT = $00000004;
7 ]. K+ M5 k3 P. L& R - DEVICE_STATEMASK_ALL = $00000007;
H' k; k4 X! p4 N9 A& D
- @3 n0 J- u# e3 E- type. P( G) m {2 c$ Y
- EDataFlow = TOleEnum;
4 z- F/ m9 @6 m0 j2 }4 u% E* x9 P E
# t k8 b4 ], I3 p% `& S- const
% U: [# r, `" o6 M* i! L - eRender = $00000000;
6 H7 B5 u6 l) E - eCapture = $00000001;. h' V! N$ W+ d
- eAll = $00000002;$ x5 u3 P8 h: ]0 ]
- EDataFlow_enum_count = $00000003;4 L5 { L5 ?3 ?9 s% s
- % W7 L5 ?" ]; a$ ^
- type
2 Y; b9 G- A0 I* L - ERole = TOleEnum;- a# X$ g/ K V2 B& k& i
! N+ O- H( f7 _7 E% i; m" F! A* Q- const4 l" t+ ?: c# ?; v+ e; c, v
- eConsole = $00000000;
p4 e: [7 D f! F2 u# W - eMultimedia = $00000001;1 Q+ g- Q) }+ I* c4 u; Q" k$ S5 {, G
- eCommunications = $00000002;9 l$ [, v) u# P( F( L
- ERole_enum_count = $00000003;
1 I) w9 i3 O; @6 y+ g
" ^0 g% [+ H; P/ f& M+ e! g- type6 I h6 H) L+ |* L& T# s3 F
- IAudioEndpointVolumeCallback = interface(IUnknown)
, A* Z9 ?, y6 ]; s* I1 A8 | - ['{657804FA-D6AD-4496-8A60-352752AF4F89}']5 T3 l7 ~7 ]7 Y7 s% ?- b: J
- end;' A6 u- |# |! F* C, r% t" w; o2 P8 ]
6 g9 b8 m2 n# c- IAudioEndpointVolume = interface(IUnknown)
( } z8 i. f8 m& \0 W - ['{5CDF2C82-841E-4546-9722-0CF74078229A}']7 Z* F! |. a8 }+ I c, L( T
- function RegisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;0 q! S& h7 o l; r( J
- function UnregisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer; stdcall;* O q G$ p% Z& S
- function GetChannelCount(out PInteger): Integer; stdcall;
( c) [( F6 @5 {; I - function SetMasterVolumeLevel(fLevelDB: single; pguidEventContext: PGUID): Integer; stdcall;
7 d5 J' q, R% g: x7 N - function SetMasterVolumeLevelScalar(fLevelDB: single; pguidEventContext: PGUID): Integer; stdcall;5 G* U( a" V" R' ] t R
- function GetMasterVolumeLevel(out fLevelDB: single): Integer; stdcall;8 Q7 p1 T, _7 \3 G# L4 V
- function GetMasterVolumeLevelScaler(out fLevelDB: single): Integer; stdcall;
; m# f5 ~2 r' ~9 k+ n) Q$ D - function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): Integer; stdcall;1 Q7 j" y; r R( j
- function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: double; pguidEventContext: PGUID): Integer; stdcall;
! {% @ E$ ?7 _( n" i+ Y6 H* R b - function GetChannelVolumeLevel(nChannel: Integer; out fLevelDB: double): Integer; stdcall;
9 E; d; x+ U- v+ P6 _ - function GetChannelVolumeLevelScalar(nChannel: Integer; out fLevel: double): Integer; stdcall;
# Q9 F: i( S% p - function SetMute(bMute: Boolean; pguidEventContext: PGUID): Integer; stdcall;
. i+ B7 e* M; v: K* G9 z+ y - function GetMute(out bMute: Boolean): Integer; stdcall;
$ k* J3 d. @' D3 a, { - function GetVolumeStepInfo(pnStep: Integer; out pnStepCount: Integer): Integer; stdcall;
, U! b7 i) n! X* i& J3 ] - function VolumeStepUp(pguidEventContext: PGUID): Integer; stdcall;
, {1 R" i7 t" Z9 A - function VolumeStepDown(pguidEventContext: PGUID): Integer; stdcall;6 l% ~' N9 P8 ?+ o
- function QueryHardwareSupport(out pdwHardwareSupportMask): Integer; stdcall;$ h! R, I1 }/ |9 Y4 r
- function GetVolumeRange(out pflVolumeMindB: double; out pflVolumeMaxdB: double; out pflVolumeIncrementdB: double): Integer; stdcall;
0 [- S3 e1 _; e+ A8 c5 b2 b - end;
, \, Y" C; n5 I) a. a# H - 2 Z: B8 u! I. ?8 [
- IAudioMeterInformation = interface(IUnknown)
( ~ y/ Z1 o4 _ - ['{C02216F6-8C67-4B5B-9D00-D008E73E0064}']4 _) {& [+ v1 J! c0 G( C
- end;; i4 _9 E2 n% p
- 7 E8 P7 ~/ i- j
- IPropertyStore = interface(IUnknown)
0 a3 L/ e2 C7 w9 t B6 z - end;& R) `3 t, J) q+ Q7 U: l$ }
- / ~1 R& b7 E3 a# K
- IMMDevice = interface(IUnknown)
$ d ?- z/ B; B+ p, N+ r6 m' t - ['{D666063F-1587-4E43-81F1-B948E807363F}'] r6 l7 i8 K5 C$ \3 t
- function Activate(const refId: TGUID;
6 g; |. w) `! _% n8 z5 U& C - dwClsCtx: DWORD;) Q' e. M/ F9 |9 s- u9 p
- pActivationParams: PInteger;
1 o8 J1 U* z( S - out pEndpointVolume: IAudioEndpointVolume): Hresult; stdCall;7 ]! x( m' F+ X0 }
- function OpenPropertyStore(stgmAccess: DWORD; out ppProperties: IPropertyStore): Hresult; stdcall;
+ y& k2 H+ M1 ^# B" B ]7 k - function GetId(out ppstrId: PLPWSTR): Hresult; stdcall;
/ U1 x6 x |' r$ ~ - function GetState(out State: Integer): Hresult; stdcall;$ n" k0 G" z$ [: }$ n
- end;; o2 B5 x! ^ d* W9 y# I
! Z; y: G y+ C2 @" e
: ~7 ?+ K+ z) ] V) U- IMMDeviceCollection = interface(IUnknown)
9 I: O% e0 e H) ` - ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']7 b( T, Y7 S" V: [$ S0 R( n* X
- end;3 `+ e% P q* j4 C0 k, s
- | o$ D0 S( g5 B- IMMNotificationClient = interface(IUnknown)
; R# L( E. ]7 F& H1 y- n - ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']
* a3 `/ T% D6 L, l- E' h - end;4 e$ c+ d" m8 j5 ]. K
" D& v! |4 b4 i: M- IMMDeviceEnumerator = interface(IUnknown)
$ {0 o9 n" F! C - ['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
/ x+ S. A* M# x& j' J" I. f - function EnumAudioEndpoints(dataFlow: EDataFlow; deviceState: SYSUINT; DevCollection: IMMDeviceCollection): Hresult; stdcall;0 }: b& r5 I) M% n3 a
- function GetDefaultAudioEndpoint(EDF: SYSUINT; ER: SYSUINT; out Dev :IMMDevice ): Hresult; stdcall;
7 Z. G, W* K. \/ |7 y - function GetDevice(pwstrId: pointer; out Dev: IMMDevice): HResult; stdcall;
) ~* w$ v3 y& ~6 \2 F6 Z - function RegisterEndpointNotificationCallback(pClient: IMMNotificationClient): Hresult; stdcall;0 c; u' k( h4 D0 C3 w7 @5 v9 w
- end;6 M: }- K; r4 F! \
- 0 r/ ^" j3 c8 u6 e& F( J: c
- implementation# I% }. n6 x w+ p
4 G, \* ]! d3 a( {. f/ Z5 e; @) O- end.
/ [6 L; v# X" e' O0 } - # s. R) V- l" [
- ///////////////////////
( z0 { R O3 n - simple sample :)
4 c: s1 e7 o: P2 E* g: \6 x - ///////////////////////
( h; _# u. P4 \% b - W. `# @5 D' `: @) G/ @$ o
- //...... other code+ `( M+ [: M5 C. h$ K; S1 `' q
- 2 H+ H+ }, j( R' l9 ^1 v8 k! @
- uses ... ActiveX, MMDevApi, ...;: Q% W3 f/ C/ V- H3 R/ P0 o9 ]
# Z0 I! y' V/ ~1 K& B; h ^- //...... other code/ U, M C' ?% H6 M* h# Z
- & ~, {" X3 n+ e0 D. s8 P9 h6 k2 Y
- var4 Q8 C D! V, p
- endpointVolume: IAudioEndpointVolume = nil;' H8 R) b4 @8 s, y2 k
( n+ K7 y! H ~* v, {* |- procedure TForm1.FormCreate(Sender: TObject);
( k$ Q K+ w, U# Y& w6 u4 y - var6 F8 H& J1 X- }. }* e- n0 I
- deviceEnumerator: IMMDeviceEnumerator;1 F6 d& C6 U7 A2 w9 J
- defaultDevice: IMMDevice;$ l% w/ O! q. A/ {
- begin
4 h \7 x( W- J - CoCreateInstance(CLASS_IMMDeviceEnumerator, nil, CLSCTX_INPROC_SERVER, IID_IMMDeviceEnumerator, deviceEnumerator);
; A7 Z, \+ H/ ^; b - deviceEnumerator.GetDefaultAudioEndpoint(eRender, eConsole, defaultDevice);
8 o8 ^8 k- J$ t' _ - defaultDevice.Activate(IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, nil, endpointVolume);) l9 Y, S( w" g3 G
- end;6 a& z3 Z1 s/ K
- , ^0 n7 j3 H, X. X" z
- procedure TForm1.Button1Click(Sender: TObject);( P: T2 b( z# u3 J9 ?& {
- var
/ `! G8 x) q; Y4 p: D' R - VolumeLevel: Single;
$ l/ @7 b0 X* t3 r( F - begin
4 T- I f3 @" ~/ h - if endpointVolume = nil then Exit;
/ C6 r: l4 o- F% | R6 v9 f N3 q: b - VolumeLevel := 0.50;
9 H6 N) `' h3 s, b: o - endpointVolume.SetMasterVolumeLevelScalar(VolumeLevel, nil);) H* z3 {5 _, ~0 L# I
- Caption := Format('%1.8f', [VolumeLevel])
( j0 a+ \$ @+ L - end;
" Q" I& M8 {1 [2 f
+ E& q4 l) V' s. ~7 t) }8 r- /////////////////////////////////////$ v1 x% i. Q! Y
- . I1 P; E$ x, w
- // with best regards ToxicDream6 ?! R; X7 i) p( s; C3 `2 @/ q
复制代码 |
|