C# 获取音频进程列表

标签 c# .net audio process

我正在尝试获取具有音频的进程列表。
Volume Mixer 中显示的进程。

目前我有:

var currentProcess = Process.GetProcesses();
foreach (Process Proc in currentProcess.Where(p => p.MainWindowHandle != IntPtr.Zero))
{
     Console.WriteLine(Proc.ProcessName);
}

哪些列表应用程序有一个窗口,有没有办法进一步过滤它以仅显示音频进程?

谢谢!

最佳答案

感谢这个 post
我能够修改其中一种方法以返回所有音频进程

    public static List<Process> GetAudioProcesses()
    {
        IMMDeviceEnumerator deviceEnumerator = null;
        IAudioSessionEnumerator sessionEnumerator = null;
        IAudioSessionManager2 mgr = null;
        IMMDevice speakers = null;
        List<Process> audioProcesses = new List<Process>();
        try
        {
            // get the speakers (1st render + multimedia) device
            deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            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);
            mgr = (IAudioSessionManager2)o;

            // enumerate sessions for on this device
            mgr.GetSessionEnumerator(out sessionEnumerator);
            int count;
            sessionEnumerator.GetCount(out count);

            // search for an audio session with the required process-id
            for (int i = 0; i < count; ++i)
            {
                IAudioSessionControl2 ctl = null;
                try
                {
                    sessionEnumerator.GetSession(i, out ctl);
                    ctl.GetProcessId(out int cpid);

                    audioProcesses.Add(Process.GetProcessById(cpid));
                }
                finally
                {
                    if (ctl != null) Marshal.ReleaseComObject(ctl);
                }
            }

            return audioProcesses;
        }
        finally
        {
            if (sessionEnumerator != null) Marshal.ReleaseComObject(sessionEnumerator);
            if (mgr != null) Marshal.ReleaseComObject(mgr);
            if (speakers != null) Marshal.ReleaseComObject(speakers);
            if (deviceEnumerator != null) Marshal.ReleaseComObject(deviceEnumerator);
        }
    }

关于C# 获取音频进程列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030208/

相关文章:

c# - 反序列化 JSON 字符串嵌套字典

c# - 如何避免 ViewModel 中的命令困惑?

asp.net - 如何设置 MS Chart 中 X 轴和 Y 轴的比例?

C# - 在字符串列表列表中重复而不是正确的值

android - super 强大的 Android 同时播放和录制

ios - 如何使 slider 值改变对象或函数?

IOS 5 使用 buzztouch 应用程序以静音模式播放视频时没有声音

c# - c# 多个窗体之间的通信

c# - 属性和引用

.net - Web 应用程序尝试在 ASP.NET MVC4 应用程序中加载 System.Web.Mvc,版本=1.0.0.0