Пример #1
0
        public static SoundInfo[] GetCurrentSoundInfo()
        {
            SoundInfo[] soundSourceInfos = new SoundInfo[0];

            try
            { // TODO: properly handle when headphones are used, etc.
                MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

                List<SoundInfo> soundSourceInfoList = new List<SoundInfo>();
                SoundInfo soundInfo = null;

                // Note the AudioSession manager did not have a method to enumerate all sessions in windows Vista; this will only work on Win7 and newer.
                for (int i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
                {
                    AudioSessionControl session = device.AudioSessionManager.Sessions[i];

                    soundInfo = new SoundInfo();

            //                    if (session.State == AudioSessionState.AudioSessionStateActive)
                    {
                        soundInfo.Pid = (int)session.ProcessID;

                        soundInfo.IconPath = session.IconPath;
                        soundInfo.DisplayName = session.DisplayName;
                        soundInfo.SessionIdentifier = session.SessionIdentifier;
                        soundInfo.SessionInstanceIdentifier = session.SessionInstanceIdentifier;
                        soundInfo.IsSystemSoundsSession = session.IsSystemSoundsSession;
                        //soundSourceInfo.State = session.State;

                        try
                        {
                            int pid = (int)session.ProcessID;
                            if (pid != 0)
                            {
                                string procName;
                                if (false == ProcNameDict.TryGetValue(pid, out procName))
                                {
                                    try
                                    {
                                        Process p = Process.GetProcessById(pid); //TO_DO: should remove processname and windowtitle from this class (but make sure that windowtitle gets updated at appropriate interval)
                                        ProcNameDict[pid] = p.ProcessName;
                                        ProcWindowTitleDict[pid] = p.MainWindowTitle;
                                        try
                                        {
                                            if (p.Modules.Count > 0)
                                                ProcFullPathDict[pid] = p.Modules[0].FileName;
                                            else
                                                ProcFullPathDict[pid] = "";
                                        }
                                        catch (Exception ex)
                                        {
                                            // WMI code from stackoverflow
                                            string query = "SELECT ExecutablePath, ProcessID FROM Win32_Process";
                                            System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);

                                            foreach (ManagementObject item in searcher.Get())
                                            {
                                                object id = item["ProcessID"];
                                                object path = item["ExecutablePath"];

                                                if (path != null && id.ToString() == p.Id.ToString())
                                                {
                                                    ProcFullPathDict[pid] = path.ToString();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        System.Diagnostics.Debug.WriteLine(ex);
                                        ProcNameDict[pid] = "";
                                        ProcWindowTitleDict[pid] = "";
                                        ProcFullPathDict[pid] = "";
                                    }
                                }
                                soundInfo.ProcessName = ProcNameDict[pid];

                                soundInfo.WindowTitle = ProcWindowTitleDict[pid];
                                if (soundInfo.WindowTitle == "")
                                {
                                    try
                                    {
                                        Process proc = Process.GetProcessById(pid);
                                        soundInfo.WindowTitle = proc.MainWindowTitle;
                                        if (soundInfo.WindowTitle == "")
                                        {
                                            soundInfo.WindowTitle = proc.ProcessName;
                                        }
                                    }
                                    catch { }
                                }
                                soundInfo.ProcessFullPath = ProcFullPathDict[pid];
                                if ((soundInfo.ProcessFullPath == "") && (soundInfo.IsSystemSoundsSession == false))
                                {
                                    int x = 0;
                                    x++;
                                }
                            }
                            else
                            {
                                soundInfo.ProcessName = "";
                                soundInfo.WindowTitle = "System Sounds";
                                soundInfo.ProcessFullPath = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }

                        AudioMeterInformation mi = session.AudioMeterInformation;
                        SimpleAudioVolume vol = session.SimpleAudioVolume;

                        soundInfo.Muted = vol.Mute;
                        soundInfo.MixerVolume = session.SimpleAudioVolume.MasterVolume;
                        //session.SimpleAudioVolume.MasterVolume = soundSourceInfo.ChannelVolume;
                        soundInfo.EmittedVolume = session.AudioMeterInformation.MasterPeakValue;
                        soundSourceInfoList.Add(soundInfo);
                    }
                }

                // Free up the memory
                IntPtr pointer = Marshal.GetIUnknownForObject(device);
                Marshal.Release(pointer);

                soundSourceInfos = soundSourceInfoList.ToArray();

                Array.Sort(soundSourceInfos, delegate(SoundInfo info1, SoundInfo info2)
                {
                    return info1.CompareTo(info2);
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
            }

            return soundSourceInfos;
        }
Пример #2
0
        public static SoundInfo[] GetCurrentSoundInfo()
        {
            SoundInfo[] soundSourceInfos = new SoundInfo[0];

            try
            { // TODO: properly handle when headphones are used, etc.
                MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                MMDevice           device  = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

                List <SoundInfo> soundSourceInfoList = new List <SoundInfo>();
                SoundInfo        soundInfo           = null;

                // Note the AudioSession manager did not have a method to enumerate all sessions in windows Vista; this will only work on Win7 and newer.
                for (int i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
                {
                    AudioSessionControl session = device.AudioSessionManager.Sessions[i];

                    soundInfo = new SoundInfo();

//                    if (session.State == AudioSessionState.AudioSessionStateActive)
                    {
                        soundInfo.Pid = (int)session.ProcessID;

                        soundInfo.IconPath                  = session.IconPath;
                        soundInfo.DisplayName               = session.DisplayName;
                        soundInfo.SessionIdentifier         = session.SessionIdentifier;
                        soundInfo.SessionInstanceIdentifier = session.SessionInstanceIdentifier;
                        soundInfo.IsSystemSoundsSession     = session.IsSystemSoundsSession;
                        //soundSourceInfo.State = session.State;

                        try
                        {
                            int pid = (int)session.ProcessID;
                            if (pid != 0)
                            {
                                string procName;
                                if (false == ProcNameDict.TryGetValue(pid, out procName))
                                {
                                    try
                                    {
                                        Process p = Process.GetProcessById(pid); //TO_DO: should remove processname and windowtitle from this class (but make sure that windowtitle gets updated at appropriate interval)
                                        ProcNameDict[pid]        = p.ProcessName;
                                        ProcWindowTitleDict[pid] = p.MainWindowTitle;
                                        try
                                        {
                                            if (p.Modules.Count > 0)
                                            {
                                                ProcFullPathDict[pid] = p.Modules[0].FileName;
                                            }
                                            else
                                            {
                                                ProcFullPathDict[pid] = "";
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            // WMI code from stackoverflow
                                            string query = "SELECT ExecutablePath, ProcessID FROM Win32_Process";
                                            System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);

                                            foreach (ManagementObject item in searcher.Get())
                                            {
                                                object id   = item["ProcessID"];
                                                object path = item["ExecutablePath"];

                                                if (path != null && id.ToString() == p.Id.ToString())
                                                {
                                                    ProcFullPathDict[pid] = path.ToString();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        System.Diagnostics.Debug.WriteLine(ex);
                                        ProcNameDict[pid]        = "";
                                        ProcWindowTitleDict[pid] = "";
                                        ProcFullPathDict[pid]    = "";
                                    }
                                }
                                soundInfo.ProcessName = ProcNameDict[pid];

                                soundInfo.WindowTitle = ProcWindowTitleDict[pid];
                                if (soundInfo.WindowTitle == "")
                                {
                                    try
                                    {
                                        Process proc = Process.GetProcessById(pid);
                                        soundInfo.WindowTitle = proc.MainWindowTitle;
                                        if (soundInfo.WindowTitle == "")
                                        {
                                            soundInfo.WindowTitle = proc.ProcessName;
                                        }
                                    }
                                    catch { }
                                }
                                soundInfo.ProcessFullPath = ProcFullPathDict[pid];
                                if ((soundInfo.ProcessFullPath == "") && (soundInfo.IsSystemSoundsSession == false))
                                {
                                    int x = 0;
                                    x++;
                                }
                            }
                            else
                            {
                                soundInfo.ProcessName     = "";
                                soundInfo.WindowTitle     = "System Sounds";
                                soundInfo.ProcessFullPath = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }

                        AudioMeterInformation mi  = session.AudioMeterInformation;
                        SimpleAudioVolume     vol = session.SimpleAudioVolume;

                        soundInfo.Muted       = vol.Mute;
                        soundInfo.MixerVolume = session.SimpleAudioVolume.MasterVolume;
                        //session.SimpleAudioVolume.MasterVolume = soundSourceInfo.ChannelVolume;
                        soundInfo.EmittedVolume = session.AudioMeterInformation.MasterPeakValue;
                        soundSourceInfoList.Add(soundInfo);
                    }
                }

                // Free up the memory
                IntPtr pointer = Marshal.GetIUnknownForObject(device);
                Marshal.Release(pointer);

                soundSourceInfos = soundSourceInfoList.ToArray();

                Array.Sort(soundSourceInfos, delegate(SoundInfo info1, SoundInfo info2)
                {
                    return(info1.CompareTo(info2));
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
            }

            return(soundSourceInfos);
        }