C#获取音频处理列表

时间:2018-08-26 20:58:20

标签: c# .net audio process

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

目前,我有:

member.PairMembers
        .Select(p => p.Pair.PairMembers.Where(m => m.MemberId != member.Id))

哪些列表应用程序带有窗口,是否有一种方法可以进一步过滤此过滤器以仅显示音频过程?

谢谢!

1 个答案:

答案 0 :(得分:1)

感谢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);
        }
    }