在启用了麦克风的Windows 10 Pro上执行此代码会引发异常。
知道为什么吗?
using Microsoft.Speech.Recognition;
...
static void Main(string[] args)
{
// Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-GB")))
{
// Create a grammar for finding services in different cities.
Choices locations = new Choices(new string[] { "office" });
Choices devices = new Choices(new string[] { "lights" ,"shades"});
Choices actions = new Choices(new string[] { "off" , "on", "up", "down"});
GrammarBuilder findServices = new GrammarBuilder("Jarvis");
findServices.Append(locations);
findServices.Append(devices);
findServices.Append(actions);
// Create a Grammar object from the GrammarBuilder and load it to the recognizer.
Grammar servicesGrammar = new Grammar(findServices);
recognizer.LoadGrammarAsync(servicesGrammar);
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure the input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// throws:
// An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Speech.dll
// Cannot find the requested data item, such as a data key or value.
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Keep the console window open.
while (true)
{
Console.ReadLine();
}
}
}
例外:
'System.InvalidOperationException'
{"Cannot find the requested data item, such as a data key or value."}
at Microsoft.Speech.Recognition.RecognizerBase.SetInputToDefaultAudioDevice()
at Napoleon.Program.Main(String[] args) in C:\voice_recognition\Program.cs:line 42
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()