public static IEnumerable<ApplicationVolumeInformation> EnumerateApplications() { // 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(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); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); uint GetProcessID=0; String GetName = ""; float GetVolume = 0; String GetIconPath = ""; IAudioSessionControl getsession = null; getsession = ctl; if (ctl is IAudioSessionControl2) { IAudioSessionControl2 ctl2 = ((IAudioSessionControl2)ctl); ctl2.GetProcessId(out GetProcessID); ctl2.GetDisplayName(out GetName); String sIconPath; ctl2.GetIconPath(out sIconPath); ISimpleAudioVolume volcast = (ctl2 as ISimpleAudioVolume); float grabvolume; volcast.GetMasterVolume(out grabvolume); GetVolume = grabvolume; Process grabProcess = Process.GetProcessById((int)GetProcessID); if(String.IsNullOrEmpty(GetName)) { GetName = grabProcess.ProcessName; } } ApplicationVolumeInformation avi = new ApplicationVolumeInformation(getsession,GetProcessID,GetVolume,GetName,GetIconPath); yield return avi; Marshal.ReleaseComObject(ctl); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); }
public static IEnumerable <ApplicationVolumeInformation> EnumerateApplications() { // 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(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); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); uint GetProcessID = 0; String GetName = ""; float GetVolume = 0; String GetIconPath = ""; IAudioSessionControl getsession = null; getsession = ctl; if (ctl is IAudioSessionControl2) { IAudioSessionControl2 ctl2 = ((IAudioSessionControl2)ctl); ctl2.GetProcessId(out GetProcessID); ctl2.GetDisplayName(out GetName); String sIconPath; ctl2.GetIconPath(out sIconPath); ISimpleAudioVolume volcast = (ctl2 as ISimpleAudioVolume); float grabvolume; volcast.GetMasterVolume(out grabvolume); GetVolume = grabvolume; try { Process grabProcess = Process.GetProcessById((int)GetProcessID); if (String.IsNullOrEmpty(GetName)) { GetName = grabProcess.ProcessName; } } catch (Exception exx) { GetName = "Name Not Available"; } } ApplicationVolumeInformation avi = new ApplicationVolumeInformation(getsession, GetProcessID, GetVolume, GetName, GetIconPath); yield return(avi); Marshal.ReleaseComObject(ctl); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); }