@@ -15,6 +15,7 @@ internal static class ProcessVolume
15
15
internal static float ? GetApplicationVolume ( string processName )
16
16
{
17
17
ISimpleAudioVolume ? volume = getVolumeObject ( processName ) ;
18
+
18
19
if ( volume == null )
19
20
return null ;
20
21
@@ -26,6 +27,7 @@ internal static class ProcessVolume
26
27
internal static bool ? GetApplicationMute ( string processName )
27
28
{
28
29
ISimpleAudioVolume ? volume = getVolumeObject ( processName ) ;
30
+
29
31
if ( volume == null )
30
32
return null ;
31
33
@@ -37,6 +39,7 @@ internal static class ProcessVolume
37
39
internal static void SetApplicationVolume ( string processName , float percentage )
38
40
{
39
41
ISimpleAudioVolume ? volume = getVolumeObject ( processName ) ;
42
+
40
43
if ( volume == null )
41
44
return ;
42
45
@@ -48,6 +51,7 @@ internal static void SetApplicationVolume(string processName, float percentage)
48
51
internal static void SetApplicationMute ( string processName , bool mute )
49
52
{
50
53
ISimpleAudioVolume ? volume = getVolumeObject ( processName ) ;
54
+
51
55
if ( volume == null )
52
56
return ;
53
57
@@ -58,169 +62,176 @@ internal static void SetApplicationMute(string processName, bool mute)
58
62
59
63
private static ISimpleAudioVolume ? getVolumeObject ( string processName )
60
64
{
61
- // get the speakers (1st render + multimedia) device
62
- IMMDeviceEnumerator deviceEnumerator = ( IMMDeviceEnumerator ) new MMDeviceEnumerator ( ) ;
63
- deviceEnumerator . GetDefaultAudioEndpoint ( EDataFlow . eRender , ERole . eMultimedia , out var speakers ) ;
64
-
65
- // activate the session manager. we need the enumerator
66
- Guid IID_IAudioSessionManager2 = typeof ( IAudioSessionManager2 ) . GUID ;
67
- speakers . Activate ( ref IID_IAudioSessionManager2 , 0 , IntPtr . Zero , out var o ) ;
68
- IAudioSessionManager2 mgr = ( IAudioSessionManager2 ) o ;
65
+ try
66
+ {
67
+ // get the speakers (1st render + multimedia) device
68
+ IMMDeviceEnumerator deviceEnumerator = ( IMMDeviceEnumerator ) new MMDeviceEnumerator ( ) ;
69
+ deviceEnumerator . GetDefaultAudioEndpoint ( EDataFlow . eRender , ERole . eMultimedia , out var speakers ) ;
69
70
70
- // enumerate sessions for on this device
71
- mgr . GetSessionEnumerator ( out var sessionEnumerator ) ;
72
- sessionEnumerator . GetCount ( out var count ) ;
71
+ // activate the session manager. we need the enumerator
72
+ Guid IID_IAudioSessionManager2 = typeof ( IAudioSessionManager2 ) . GUID ;
73
+ speakers . Activate ( ref IID_IAudioSessionManager2 , 0 , IntPtr . Zero , out var o ) ;
74
+ IAudioSessionManager2 mgr = ( IAudioSessionManager2 ) o ;
73
75
74
- // search for an audio session with the required name
75
- // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
76
- ISimpleAudioVolume ? volumeControl = null ;
76
+ // enumerate sessions for on this device
77
+ mgr . GetSessionEnumerator ( out var sessionEnumerator ) ;
78
+ sessionEnumerator . GetCount ( out var count ) ;
77
79
78
- for ( int i = 0 ; i < count ; i ++ )
79
- {
80
- sessionEnumerator . GetSession ( i , out var ctl ) ;
81
- ctl . GetSessionIdentifier ( out var identifier ) ;
80
+ // search for an audio session with the required name
81
+ // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
82
+ ISimpleAudioVolume ? volumeControl = null ;
82
83
83
- if ( identifier . Contains ( processName , StringComparison . InvariantCultureIgnoreCase ) )
84
+ for ( int i = 0 ; i < count ; i ++ )
84
85
{
85
- volumeControl = ctl as ISimpleAudioVolume ;
86
- break ;
86
+ sessionEnumerator . GetSession ( i , out var ctl ) ;
87
+ ctl . GetSessionIdentifier ( out var identifier ) ;
88
+
89
+ if ( identifier . Contains ( processName , StringComparison . InvariantCultureIgnoreCase ) )
90
+ {
91
+ volumeControl = ctl as ISimpleAudioVolume ;
92
+ break ;
93
+ }
94
+
95
+ Marshal . ReleaseComObject ( ctl ) ;
87
96
}
88
97
89
- Marshal . ReleaseComObject ( ctl ) ;
98
+ Marshal . ReleaseComObject ( sessionEnumerator ) ;
99
+ Marshal . ReleaseComObject ( mgr ) ;
100
+ Marshal . ReleaseComObject ( speakers ) ;
101
+ Marshal . ReleaseComObject ( deviceEnumerator ) ;
102
+ return volumeControl ;
103
+ }
104
+ catch
105
+ {
106
+ return null ;
90
107
}
91
-
92
- Marshal . ReleaseComObject ( sessionEnumerator ) ;
93
- Marshal . ReleaseComObject ( mgr ) ;
94
- Marshal . ReleaseComObject ( speakers ) ;
95
- Marshal . ReleaseComObject ( deviceEnumerator ) ;
96
- return volumeControl ;
97
108
}
98
- }
99
109
100
- [ ComImport ]
101
- [ Guid ( "BCDE0395-E52F-467C-8E3D-C4579291692E" ) ]
102
- internal class MMDeviceEnumerator
103
- {
104
- }
110
+ [ ComImport ]
111
+ [ Guid ( "BCDE0395-E52F-467C-8E3D-C4579291692E" ) ]
112
+ internal class MMDeviceEnumerator
113
+ {
114
+ }
105
115
106
- internal enum EDataFlow
107
- {
108
- eRender ,
109
- eCapture ,
110
- eAll ,
111
- EDataFlow_enum_count
112
- }
116
+ internal enum EDataFlow
117
+ {
118
+ eRender ,
119
+ eCapture ,
120
+ eAll ,
121
+ EDataFlow_enum_count
122
+ }
113
123
114
- internal enum ERole
115
- {
116
- eConsole ,
117
- eMultimedia ,
118
- eCommunications ,
119
- ERole_enum_count
120
- }
124
+ internal enum ERole
125
+ {
126
+ eConsole ,
127
+ eMultimedia ,
128
+ eCommunications ,
129
+ ERole_enum_count
130
+ }
121
131
122
- [ Guid ( "A95664D2-9614-4F35-A746-DE8DB63617E6" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
123
- internal interface IMMDeviceEnumerator
124
- {
125
- int NotImpl1 ( ) ;
132
+ [ Guid ( "A95664D2-9614-4F35-A746-DE8DB63617E6" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
133
+ internal interface IMMDeviceEnumerator
134
+ {
135
+ int NotImpl1 ( ) ;
126
136
127
- [ PreserveSig ]
128
- int GetDefaultAudioEndpoint ( EDataFlow dataFlow , ERole role , out IMMDevice ppDevice ) ;
137
+ [ PreserveSig ]
138
+ int GetDefaultAudioEndpoint ( EDataFlow dataFlow , ERole role , out IMMDevice ppDevice ) ;
129
139
130
- // the rest is not implemented
131
- }
140
+ // the rest is not implemented
141
+ }
132
142
133
- [ Guid ( "D666063F-1587-4E43-81F1-B948E807363F" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
134
- internal interface IMMDevice
135
- {
136
- [ PreserveSig ]
137
- int Activate ( ref Guid iid , int dwClsCtx , IntPtr pActivationParams , [ MarshalAs ( UnmanagedType . IUnknown ) ] out object ppInterface ) ;
143
+ [ Guid ( "D666063F-1587-4E43-81F1-B948E807363F" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
144
+ internal interface IMMDevice
145
+ {
146
+ [ PreserveSig ]
147
+ int Activate ( ref Guid iid , int dwClsCtx , IntPtr pActivationParams , [ MarshalAs ( UnmanagedType . IUnknown ) ] out object ppInterface ) ;
138
148
139
- // the rest is not implemented
140
- }
149
+ // the rest is not implemented
150
+ }
141
151
142
- [ Guid ( "77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
143
- internal interface IAudioSessionManager2
144
- {
145
- int NotImpl1 ( ) ;
146
- int NotImpl2 ( ) ;
152
+ [ Guid ( "77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
153
+ internal interface IAudioSessionManager2
154
+ {
155
+ int NotImpl1 ( ) ;
156
+ int NotImpl2 ( ) ;
147
157
148
- [ PreserveSig ]
149
- int GetSessionEnumerator ( out IAudioSessionEnumerator SessionEnum ) ;
158
+ [ PreserveSig ]
159
+ int GetSessionEnumerator ( out IAudioSessionEnumerator SessionEnum ) ;
150
160
151
- // the rest is not implemented
152
- }
161
+ // the rest is not implemented
162
+ }
153
163
154
- [ Guid ( "E2F5BB11-0570-40CA-ACDD-3AA01277DEE8" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
155
- internal interface IAudioSessionEnumerator
156
- {
157
- [ PreserveSig ]
158
- int GetCount ( out int SessionCount ) ;
164
+ [ Guid ( "E2F5BB11-0570-40CA-ACDD-3AA01277DEE8" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
165
+ internal interface IAudioSessionEnumerator
166
+ {
167
+ [ PreserveSig ]
168
+ int GetCount ( out int SessionCount ) ;
159
169
160
- [ PreserveSig ]
161
- int GetSession ( int SessionCount , out IAudioSessionControl2 Session ) ;
162
- }
170
+ [ PreserveSig ]
171
+ int GetSession ( int SessionCount , out IAudioSessionControl2 Session ) ;
172
+ }
163
173
164
- [ Guid ( "87CE5498-68D6-44E5-9215-6DA47EF883D8" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
165
- internal interface ISimpleAudioVolume
166
- {
167
- [ PreserveSig ]
168
- int SetMasterVolume ( float fLevel , ref Guid EventContext ) ;
174
+ [ Guid ( "87CE5498-68D6-44E5-9215-6DA47EF883D8" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
175
+ internal interface ISimpleAudioVolume
176
+ {
177
+ [ PreserveSig ]
178
+ int SetMasterVolume ( float fLevel , ref Guid EventContext ) ;
169
179
170
- [ PreserveSig ]
171
- int GetMasterVolume ( out float pfLevel ) ;
180
+ [ PreserveSig ]
181
+ int GetMasterVolume ( out float pfLevel ) ;
172
182
173
- [ PreserveSig ]
174
- int SetMute ( bool bMute , ref Guid EventContext ) ;
183
+ [ PreserveSig ]
184
+ int SetMute ( bool bMute , ref Guid EventContext ) ;
175
185
176
- [ PreserveSig ]
177
- int GetMute ( out bool pbMute ) ;
178
- }
186
+ [ PreserveSig ]
187
+ int GetMute ( out bool pbMute ) ;
188
+ }
179
189
180
- [ Guid ( "bfb7ff88-7239-4fc9-8fa2-07c950be9c6d" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
181
- internal interface IAudioSessionControl2
182
- {
183
- // IAudioSessionControl
184
- [ PreserveSig ]
185
- int NotImpl0 ( ) ;
190
+ [ Guid ( "bfb7ff88-7239-4fc9-8fa2-07c950be9c6d" ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) ]
191
+ internal interface IAudioSessionControl2
192
+ {
193
+ // IAudioSessionControl
194
+ [ PreserveSig ]
195
+ int NotImpl0 ( ) ;
186
196
187
- [ PreserveSig ]
188
- int GetDisplayName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
197
+ [ PreserveSig ]
198
+ int GetDisplayName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
189
199
190
- [ PreserveSig ]
191
- int SetDisplayName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string Value , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
200
+ [ PreserveSig ]
201
+ int SetDisplayName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string Value , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
192
202
193
- [ PreserveSig ]
194
- int GetIconPath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
203
+ [ PreserveSig ]
204
+ int GetIconPath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
195
205
196
- [ PreserveSig ]
197
- int SetIconPath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string Value , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
206
+ [ PreserveSig ]
207
+ int SetIconPath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string Value , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
198
208
199
- [ PreserveSig ]
200
- int GetGroupingParam ( out Guid pRetVal ) ;
209
+ [ PreserveSig ]
210
+ int GetGroupingParam ( out Guid pRetVal ) ;
201
211
202
- [ PreserveSig ]
203
- int SetGroupingParam ( [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid Override , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
212
+ [ PreserveSig ]
213
+ int SetGroupingParam ( [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid Override , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid EventContext ) ;
204
214
205
- [ PreserveSig ]
206
- int NotImpl1 ( ) ;
215
+ [ PreserveSig ]
216
+ int NotImpl1 ( ) ;
207
217
208
- [ PreserveSig ]
209
- int NotImpl2 ( ) ;
218
+ [ PreserveSig ]
219
+ int NotImpl2 ( ) ;
210
220
211
- // IAudioSessionControl2
212
- [ PreserveSig ]
213
- int GetSessionIdentifier ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
221
+ // IAudioSessionControl2
222
+ [ PreserveSig ]
223
+ int GetSessionIdentifier ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
214
224
215
- [ PreserveSig ]
216
- int GetSessionInstanceIdentifier ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
225
+ [ PreserveSig ]
226
+ int GetSessionInstanceIdentifier ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pRetVal ) ;
217
227
218
- [ PreserveSig ]
219
- int GetProcessId ( out int pRetVal ) ;
228
+ [ PreserveSig ]
229
+ int GetProcessId ( out int pRetVal ) ;
220
230
221
- [ PreserveSig ]
222
- int IsSystemSoundsSession ( ) ;
231
+ [ PreserveSig ]
232
+ int IsSystemSoundsSession ( ) ;
223
233
224
- [ PreserveSig ]
225
- int SetDuckingPreference ( bool optOut ) ;
234
+ [ PreserveSig ]
235
+ int SetDuckingPreference ( bool optOut ) ;
236
+ }
226
237
}
0 commit comments