MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Изменение громкости другого приложения"
Answer 903387
У WASAPI-сессий имя часто пустое или бессмысленное. Для идентификации сессий конкретного приложения лучше использовать ID процесса:
private static ISimpleAudioVolume GetVolumeObject(int pid) { // get the speakers (1st render + multimedia) device IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); // activate the session manager. we need the enumerator Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); uint val; ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); IAudioSessionControl2 ctl2 = (IAudioSessionControl2)ctl; ctl2.GetProcessId(out val); if (val == pid) { volumeControl = ctl as ISimpleAudioVolume; break; } Marshal.ReleaseComObject(ctl); Marshal.ReleaseComObject(ctl2); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); return volumeControl; }
Интерфейс IAudioSessionControl2:
[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IAudioSessionControl2 { [PreserveSig] int GetState(out object state); [PreserveSig] int GetDisplayName(out IntPtr name); [PreserveSig] int SetDisplayName(string value, Guid EventContext); [PreserveSig] int GetIconPath(out IntPtr Path); [PreserveSig] int SetIconPath(string Value, Guid EventContext); [PreserveSig] int GetGroupingParam(out Guid GroupingParam); [PreserveSig] int SetGroupingParam(Guid Override, Guid Eventcontext); [PreserveSig] int RegisterAudioSessionNotification(object NewNotifications); [PreserveSig] int UnregisterAudioSessionNotification(object NewNotifications); [PreserveSig] int GetSessionIdentifier(out IntPtr retVal); [PreserveSig] int GetSessionInstanceIdentifier(out IntPtr retVal); [PreserveSig] int GetProcessId(out UInt32 retvVal); [PreserveSig] int IsSystemSoundsSession(); [PreserveSig] int SetDuckingPreference(bool optOut); }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.