Get processname or ID from CSCore audiostream

回眸只為那壹抹淺笑 提交于 2020-01-16 04:05:29

问题


I have filtered all audio streams from my programs with CSCore lib. It works fine, but I have no idear which stream belong to which process. Is there a way to get the processId oder processname per stream?

my code:

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;
            }
        }
    }
}

thanks a lot!


回答1:


You have to query an instance of the AudioSessionControl2 object:

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;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/27297577/get-processname-or-id-from-cscore-audiostream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!