如何在C# Windows Form项目中使用Microsoft Bing Speech API
在C# WinForms项目中使用Bing Speech API的实现指南
嘿,刚好之前我把WPF版本的Bing Speech示例迁移到WinForms项目过,其实核心逻辑完全通用,只是UI层面的处理需要适配WinForms的规则而已,我给你一步步拆解怎么操作:
首先,先搞定依赖包——不管WPF还是WinForms,都用同一个官方SDK:
- 打开NuGet包管理器,搜索
Microsoft.CognitiveServices.Speech并安装; - 或者在Package Manager Console里执行命令:
Install-Package Microsoft.CognitiveServices.Speech
1. 语音识别(麦克风输入)示例
我们用WinForms的按钮触发识别,文本框显示结果:
在你的Form里添加两个控件:一个Button(命名为btnStartRecognition)和一个TextBox(命名为txtRecogResult),然后双击按钮写以下代码:
using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; private async void btnStartRecognition_Click(object sender, EventArgs e) { // 替换成你自己的Azure Speech服务密钥和区域(比如eastasia) var speechConfig = SpeechConfig.FromSubscription("你的API密钥", "你的服务区域"); // 绑定默认麦克风作为音频输入源 using var audioConfig = AudioConfig.FromDefaultMicrophoneInput(); using var recognizer = new SpeechRecognizer(speechConfig, audioConfig); txtRecogResult.Text = "正在监听语音..."; // 单次语音识别(如果需要持续识别,后面会说) var result = await recognizer.RecognizeOnceAsync(); // 处理识别结果 switch (result.Reason) { case ResultReason.RecognizedSpeech: txtRecogResult.Text = $"识别成功:{result.Text}"; break; case ResultReason.NoMatch: txtRecogResult.Text = $"未识别到有效语音:{NoMatchDetails.FromResult(result).Reason}"; break; case ResultReason.Canceled: var cancellation = CancellationDetails.FromResult(result); txtRecogResult.Text = $"识别被取消:{cancellation.Reason}"; if (cancellation.Reason == CancellationReason.Error) { txtRecogResult.Text += $"\n错误详情:{cancellation.ErrorDetails}"; } break; } }
2. 语音合成(文本转语音)示例
添加一个Button(btnStartSynthesis)和一个TextBox(txtInputText),用来输入要合成的文本,点击按钮播放语音:
private async void btnStartSynthesis_Click(object sender, EventArgs e) { var speechConfig = SpeechConfig.FromSubscription("你的API密钥", "你的服务区域"); // 设置合成语音的类型,比如中文女声:zh-CN-XiaoxiaoNeural speechConfig.SpeechSynthesisVoiceName = "zh-CN-XiaoxiaoNeural"; // 绑定默认扬声器作为输出 using var synthesizer = new SpeechSynthesizer(speechConfig); try { var result = await synthesizer.SpeakTextAsync(txtInputText.Text); if (result.Reason == ResultReason.SynthesizingAudioCompleted) { MessageBox.Show("语音合成完成并已播放"); } else if (result.Reason == ResultReason.Canceled) { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); MessageBox.Show($"合成失败:{cancellation.Reason}\n错误信息:{cancellation.ErrorDetails}"); } } catch (Exception ex) { MessageBox.Show($"发生异常:{ex.Message}"); } }
3. 进阶:连续语音识别
如果需要持续监听并识别语音(比如实时翻译场景),不要用RecognizeOnceAsync,而是注册事件并启动连续识别,注意WinForms里跨线程操作UI要用到Invoke:
private SpeechRecognizer _continuousRecognizer; private async void btnStartContinuousRecog_Click(object sender, EventArgs e) { var speechConfig = SpeechConfig.FromSubscription("你的API密钥", "你的服务区域"); using var audioConfig = AudioConfig.FromDefaultMicrophoneInput(); _continuousRecognizer = new SpeechRecognizer(speechConfig, audioConfig); // 临时识别结果(实时更新) _continuousRecognizer.Recognizing += (s, args) => { this.Invoke((Action)(() => { txtRecogResult.Text = $"临时结果:{args.Result.Text}"; })); }; // 最终识别结果 _continuousRecognizer.Recognized += (s, args) => { this.Invoke((Action)(() => { if (args.Result.Reason == ResultReason.RecognizedSpeech) { txtRecogResult.Text += $"\n最终结果:{args.Result.Text}"; } })); }; // 启动连续识别 await _continuousRecognizer.StartContinuousRecognitionAsync(); MessageBox.Show("已开始连续识别,点击停止按钮结束"); } private async void btnStopContinuousRecog_Click(object sender, EventArgs e) { if (_continuousRecognizer != null) { await _continuousRecognizer.StopContinuousRecognitionAsync(); _continuousRecognizer.Dispose(); _continuousRecognizer = null; txtRecogResult.Text += "\n已停止连续识别"; } }
关键注意事项
- API密钥和区域:一定要替换成你在Azure门户申请的Speech Service的密钥和对应区域(比如
eastasia、westus); - 异步处理:WinForms事件方法要加
async,避免UI线程卡顿; - 跨线程UI操作:在识别事件回调里修改UI控件时,必须用
Invoke切换到UI线程,否则会报错; - 权限:Windows 10+需要在系统设置里给应用开启麦克风访问权限。
其实WPF示例的核心就是调用Speech SDK,你只需要把WPF的UI绑定、控件操作换成WinForms的写法就行,SDK本身的逻辑完全不用改~
内容的提问来源于stack exchange,提问作者Gowdham




