从SCore audiostream获取进程名称或ID

时间:2014-12-04 15:20:24

标签: c# cscore

我使用CSCore lib过滤了程序中的所有音频流。它工作正常,但我没有想法哪个流属于哪个进程。 有没有办法在每个流中获取processId oder processname?

我的代码:

public class Audio
{
    public static void start(ListBox device, ListBox process)
    {
        using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
        {

            using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
            {

                foreach (var session in sessionEnumerator)
                {
                    using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                    {
                        device.Items.Add(audioMeterInformation.GetPeakValue());

                        process.Items.Add("here I need the processname or ID");
                    }
                }
            }
        }


    }

    private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
    {
        using (var enumerator = new MMDeviceEnumerator())
        {
            using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
            {
                var sessionManager = AudioSessionManager2.FromMMDevice(device);
                return sessionManager;
            }
        }
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:4)

您必须查询AudioSessionControl2对象的实例:

public class Audio
{
    public static void start(ListBox device, ListBox process)
    {
        using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
        {

            using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
            {
                foreach (var session in sessionEnumerator)
                {
                    using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
                    using(var session2 = session.QueryInterface<AudioSessionControl2>())
                    {
                        device.Items.Add(audioMeterInformation.GetPeakValue());
                        var processID = session2.ProcessID;
                        process.Items.Add("here I need the processname or ID");
                    }
                }
            }
        }


    }

    private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
    {
        using (var enumerator = new MMDeviceEnumerator())
        {
            using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
            {
                var sessionManager = AudioSessionManager2.FromMMDevice(device);
                return sessionManager;
            }
        }
    }
}