c# - 在 uwp 中使用语音识别时出错
问题描述
我正在制作一个 uwp 应用程序,其中包含语音搜索,我得到以下代码:
我从有关语音识别的 Windows 博客中获得了这段代码:
var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
//speechRecognizer.RecognitionQualityDegrading += speechRecognizer_RecognitionQualityDegrading;
// Add a web search grammar to the recognizer.
var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");
speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
speechRecognizer.UIOptions.ExampleText = @"Ex. 'weather for London'";
speechRecognizer.Constraints.Add(webSearchGrammar);
// Compile the constraint.
await speechRecognizer.CompileConstraintsAsync();
// Start recognition.
Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
await speechRecognizer.RecognizeWithUIAsync();
// Do something with the recognition result.
var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
await messageDialog.ShowAsync();
我从一个网站上得到了另一堆代码:
async void Button_Click_2(object sender, RoutedEventArgs e)
{
this.recognizer = new SpeechRecognizer();
await this.recognizer.CompileConstraintsAsync();
this.recognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
this.recognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(20);
this.recognizer.UIOptions.AudiblePrompt = "Say whatever you like, I'm listening";
this.recognizer.UIOptions.ExampleText = "The quick brown fox jumps over the lazy dog";
this.recognizer.UIOptions.ShowConfirmation = true;
this.recognizer.UIOptions.IsReadBackEnabled = true;
this.recognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(5);
var result = await this.recognizer.RecognizeWithUIAsync();
if (result != null)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine(
$"I have {result.Confidence} confidence that you said [{result.Text}] " +
$"and it took {result.PhraseDuration.TotalSeconds} seconds to say it " +
$"starting at {result.PhraseStartTime:g}");
var alternates = result.GetAlternates(10);
builder.AppendLine(
$"There were {alternates?.Count} alternates - listed below (if any)");
if (alternates != null)
{
foreach (var alternate in alternates)
{
builder.AppendLine(
$"Alternate {alternate.Confidence} confident you said [{alternate.Text}]");
}
}
this.txtResults.Text = builder.ToString();
}
}
SpeechRecognizer recognizer;
尽管如此,每当我运行这些代码时,它们似乎都会引发错误。 这是错误 ,任何人都可以告诉如何解决这个错误,我在网上搜索它,但没有得到任何东西......
解决方案
请尝试在开始 > 设置 > 隐私 > 语音下的 Windows 设置中启用“在线语音识别”。
并确保在您的应用功能中启用麦克风。
推荐阅读
- sql-server - 具有自定义列和行分隔符的 BCP
- linux - Jupyter notebook SSH 隧道错误“无法打开签名数据库”
- python - Python,写csv单行多列
- python - 使用阈值图像改变实际图像中的亮度值
- rust - 如何使用 Serde 解析具有多种不同类型的 YAML 文件?
- html - 1fr 网格单元扩展超过 100%
- sql-server - 删除多边形外部的几何数据
- jquery - 克隆一个 HTML 块并将其附加到另一个 div
- dll - 当符号存在时,为什么我会看到符号链接错误消息?
- c# - 在包装 C# .Net DLL 的 C++ 包装器中将 System::String^ 转换为 wchar_t