如何在C# WinForm+Azure语音转文字中实现下拉框选择输入麦克风
C# WinForm + Azure语音转文字:麦克风选择与配置实现
实现步骤说明
1. 枚举系统麦克风设备
通过调用Windows原生winmm.dll API获取系统所有可用输入麦克风设备,填充到下拉框:
waveInGetNumDevs():获取输入设备总数waveInGetDevCaps():读取单个设备的名称等详细信息FillSoundDevicesInCombobox()方法遍历设备列表,将设备信息添加到下拉框CB1
2. 持久化用户选择
利用WinForm内置的Properties.Settings保存用户的设备选择,重启应用后自动恢复:
- 窗体加载时(
Form2_Load):读取保存的设备信息,匹配下拉框选项并自动选中 - 确定按钮点击时(
button1_Click):将当前选中的设备信息保存到配置文件
3. 配置Azure语音服务使用选中设备
这是核心环节,需要在语音识别初始化代码中指定选中的麦克风设备ID(避免同名设备冲突)。以下是调整后的关键代码:
首先定义设备信息类,同时存储ID和名称:
public class AudioDeviceInfo { public uint DeviceId { get; set; } public string DeviceName { get; set; } public override string ToString() { return DeviceName; // 下拉框显示设备名称 } }
修改设备填充方法,添加完整设备信息:
public void FillSoundDevicesInCombobox() { uint devices = waveInGetNumDevs(); WAVEOUTCAPS caps = new WAVEOUTCAPS(); for (uint i = 0; i < devices; i++) { waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps)); CB1.Items.Add(new AudioDeviceInfo { DeviceId = i, DeviceName = caps.szPname }); } }
在语音识别初始化时指定设备:
// 初始化Azure语音配置 var speechConfig = SpeechConfig.FromSubscription("你的Azure订阅密钥", "服务区域"); // 获取选中的设备 var selectedDevice = CB1.SelectedItem as AudioDeviceInfo; if (selectedDevice != null) { // 设置语音识别的输入设备ID speechConfig.SetProperty(SpeechConfigPropertyId.AudioConfigDeviceId, selectedDevice.DeviceId.ToString()); } // 创建语音识别器 var recognizer = new SpeechRecognizer(speechConfig);
完整整合代码
using System; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using Microsoft.CognitiveServices.Speech; namespace WindowsFormsApp2 { public partial class Form2 : Form { [DllImport("winmm.dll", SetLastError = true)] static extern uint waveInGetNumDevs(); [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern uint waveInGetDevCaps(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 = 64)] public string szPname; public uint dwFormats; public ushort wChannels; public ushort wReserved1; public uint dwSupport; } public class AudioDeviceInfo { public uint DeviceId { get; set; } public string DeviceName { get; set; } public override string ToString() { return DeviceName; } } public void FillSoundDevicesInCombobox() { uint devices = waveInGetNumDevs(); WAVEOUTCAPS caps = new WAVEOUTCAPS(); for (uint i = 0; i < devices; i++) { waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps)); CB1.Items.Add(new AudioDeviceInfo { DeviceId = i, DeviceName = caps.szPname }); } } public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { FillSoundDevicesInCombobox(); // 加载上次保存的设备选择 if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.SelectedAudioDeviceId)) { uint savedId = uint.Parse(Properties.Settings.Default.SelectedAudioDeviceId); var item = CB1.Items.Cast<AudioDeviceInfo>().FirstOrDefault(x => x.DeviceId == savedId); if (item != null) CB1.SelectedItem = item; } checkBox1.Checked = Properties.Settings.Default.Punctuation; } private void button1_Click(object sender, EventArgs e) { var selectedDevice = CB1.SelectedItem as AudioDeviceInfo; if (selectedDevice != null) { Properties.Settings.Default.SelectedAudioDeviceId = selectedDevice.DeviceId.ToString(); Properties.Settings.Default.SelectedAudioDeviceName = selectedDevice.DeviceName; } Properties.Settings.Default.Punctuation = checkBox1.Checked; Properties.Settings.Default.Save(); Close(); } private void button2_Click(object sender, EventArgs e) { Close(); } } }
注意事项
- 需提前安装
Microsoft.CognitiveServices.SpeechNuGet包 - 替换代码中的Azure订阅密钥和服务区域
- 优先使用设备ID匹配,避免同名设备导致的识别错误
内容的提问来源于stack exchange,提问作者Babar Ali
相关产品推荐
相关产品推荐

