2014-12-04 65 views
1

我已經使用CSCore lib過濾了來自我的程序的所有音頻流。它工作正常,但我不知道哪個流屬於哪個進程。 有沒有辦法讓每個流的processId或者processname?從CSCore獲取進程名稱或ID audiostream

我的代碼:

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

非常感謝!

回答

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