获取waveIn设备的全名

Mar*_*ath 12 audio winapi

我一直在使用waveInGetDevCaps来获取waveIn设备的名称,但WAVEINCAPS结构只支持31个字符加上null,这意味着在我的计算机上,我得到的设备名称被截断:

Microphone / Line In (SigmaTel 
Microphone Array (SigmaTel High, 
Run Code Online (Sandbox Code Playgroud)

我确信必须有一种方法来获取完整的设备名称,但有谁知道那是什么?

sel*_*bie 8

是的,有一个解决方法.我已经在运输代码中多次解决了这个问题.

使用DirectSoundCapture枚举音频捕获设备.API是DirectSoundCaptureEnumerate.它将返回设备的全长名称.

当然,您可能正在考虑"这很好,但我的其余代码设置为使用Wave API,而不是DirectSound.我不想全部切换.所以如何映射返回的GUID ID DirectSoundCaptureEnumerate为WaveIn API使用的整数ID?"

解决方案是针对DirectSoundPrivate对象的CoCreateInstance(或直接从dsound.dll调用GetClassObject)以获取指向IKsPropertySet接口的指针.在此界面中,您可以获取DSound GUID到Wave ID映射.有关详细信息,请参阅此网页:

http://msdn.microsoft.com/en-us/library/bb206182(VS.85).aspx

您想使用上面列出的网页上所述的DSPROPERTY_DIRECTSOUNDDEVICE_WAVEDEVICEMAPPING.

  • 获取GUID的另一种方法是使用WAVEOUTCAPS2结构调用waveOutGetDevCaps,而不是常规的WAVEOUTCAPS结构.http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382%28v=vs.85%29.aspx (3认同)
  • 这个链接似乎被打破了.你在msdn上有另一个链接或至少文章的标题吗? (2认同)

小智 5

我完成了waveIn设备的名称,探索了MMDeviceEnumerator返回的名称。对于每个waveIn设备,当名称不完整是EnumerateAudioEndPoints之一的全名的一部分时,我使用此全名以waveIn设备的相同顺序填充组合框。

VisualBasic.NET:

   Dim wain = New WaveIn()
    Dim DeviceInfoI As WaveInCapabilities
    Dim nomedevice As String
    For de = 0 To wain.DeviceCount - 1
        DeviceInfoI = wain.GetCapabilities(de)
        nomedevice = DeviceInfoI.ProductName
        For deg = 0 To devices.Count - 1
            If InStr(devices.Item(deg).FriendlyName, nomedevice) Then
                nomedevice = devices.Item(deg).FriendlyName
                Exit For
            End If
        Next
        cmbMessaggiVocaliMIC.Items.Add(nomedevice)
    Next
    cmbMessaggiVocaliMIC.SelectedIndex = 0
    waveIn.DeviceNumber = cmbMessaggiVocaliMIC.SelectedIndex
Run Code Online (Sandbox Code Playgroud)


Zun*_*air 5

基于@Andrea Bertucelli的改进/完整的C#WPF代码

using NAudio.CoreAudioApi;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            foreach (KeyValuePair<string, MMDevice> device in GetInputAudioDevices())
            {
                Console.WriteLine("Name: {0}, State: {1}", device.Key, device.Value.State);
            }
        }

        public Dictionary<string, MMDevice> GetInputAudioDevices()
        {
            Dictionary<string, MMDevice> retVal = new Dictionary<string, MMDevice>();
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
            int waveInDevices = WaveIn.DeviceCount;
            for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
                foreach (MMDevice device in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All))
                {
                    if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
                    {
                        retVal.Add(device.FriendlyName, device);
                        break;
                    }
                }
            }

            return retVal;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)