2011-05-09 101 views
1

檢測音頻輸入和輸出設備,我想findout音頻輸入和輸出設備連接了多少到我的系統在C#.NET連接到系統

請提出。

謝謝。

+0

複製 - http://stackoverflow.com/questions/1525320/how-to-enumerate-audio-out-devices-in-c – 2011-05-09 12:11:27

回答

6

您需要訪問必要的Windows API函數。

這個類應該讓你開始 - Win32.GetSoundDevices返回設備名稱列表。在Windows SDK中查找WAVEOUTCAPS以獲取您可以獲得的其他信息的詳細信息。

public class Win32 { 
    [DllImport("winmm.dll", SetLastError=true)] 
    static extern uint waveOutGetNumDevs(); 

    [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    public static extern uint waveOutGetDevCaps(uint hwo,ref WAVEOUTCAPS pwoc,uint cbwoc); 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
     public struct WAVEOUTCAPS { 
     public ushort wMid; 
     public ushort wPid; 
     public uint vDriverVersion; 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
     public string szPname; 
     public uint dwFormats; 
     public ushort wChannels; 
     public ushort wReserved1; 
     public uint dwSupport; 
    } 

    public static string [] GetSoundDevices() { 
     uint devices = waveOutGetNumDevs(); 
     string [] result = new string[devices]; 
     WAVEOUTCAPS caps = new WAVEOUTCAPS(); 

     for(uint i = 0; i < devices; i++) { 
      waveOutGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps)); 
      result[i] = caps.szPname; 
     } 
     return result; 
    } 


} 
+0

感謝您的解決方案 – usr021986 2011-05-16 09:20:10