Необработанное исключение типа "System.UnauthorizedAccessException" в System.Speech.dll - C#
Формулировка задачи:
Когда начал компилировать такая вот ошибка вышла : "Необработанное исключение типа "System.UnauthorizedAccessException" в System.Speech.dll".
А вот код:
Что делать??
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Speech.Recognition; using System.Speech.Synthesis; class Program { public SpeechRecognitionEngine recognitionEngine; string message; SpeechSynthesizer speaker = new SpeechSynthesizer(); public static void Main() { Program prgrm = new Program(); prgrm.Init(); //process commands while (true) { prgrm.message = prgrm.recognise(); prgrm.command(prgrm.message); } } public void Init() { recognitionEngine = new SpeechRecognitionEngine(); recognitionEngine.SetInputToDefaultAudioDevice(); recognitionEngine.SpeechRecognized += (s, args) => { foreach (RecognizedWordUnit word in args.Result.Words) { if (word.Confidence > 0.5f) message += word.Text + " "; } }; recognitionEngine.LoadGrammar(new DictationGrammar()); Console.WriteLine(message); } public void speak(string message) { speaker.Rate = 1; speaker.Volume = 100; speaker.Speak(message); } public string recognise() { Console.WriteLine(message); recognitionEngine.RecognizeAsyncStop(); return message; } public void command(string message) { switch (message) { case "hi": speak("Hello Sir"); Console.WriteLine(message); break; case "jarvis Chrome": speak("Opening Google Chrome"); speak("Please Wait"); Console.WriteLine("Please Wait"); System.Diagnostics.Process.Start("google chrome.lnk"); break; case "jarvis time": speak("Sir , The Time is " + DateTime.Now.ToString("h:mm tt")); break; case "jarvis date": speak("Sir , The Date is " + DateTime.Now.Date); break; case "jarvis day": speak("Sir, The Day is " + DateTime.Now.DayOfWeek); break; case "thank you": speak("You'r welcome sir"); break; case "": break; default: Console.WriteLine(message); break; } } }
Решение задачи: «Необработанное исключение типа "System.UnauthorizedAccessException" в System.Speech.dll»
textual
Листинг программы
using System; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Speech.Recognition; using System.Speech.Synthesis; using System.Windows.Forms; namespace SpeechAPI { public partial class Form1 : Form { private SpeechRecognitionEngine recognitionEngine; private SpeechSynthesizer speaker = new SpeechSynthesizer(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { speaker.Rate = 1; speaker.Volume = 100; recognitionEngine = new SpeechRecognitionEngine(new CultureInfo("en-US")); recognitionEngine.SetInputToDefaultAudioDevice(); recognitionEngine.SpeechRecognized += RecognitionEngine_SpeechRecognized; recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("hi"))); recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("jarvis Chrome"))); recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("jarvis time"))); recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("jarvis date"))); recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("jarvis day"))); recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder("thank you"))); recognitionEngine.RecognizeAsync(RecognizeMode.Multiple); } private void RecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { if (e.Result.Words.All(w => w.Confidence > 0.4f)) Command(e.Result.Text); } public void Command(string message) { switch (message) { case "hi": speaker.Speak("Hello Sir"); break; case "jarvis Chrome": speaker.Speak("Opening Google Chrome"); speaker.Speak("Please Wait"); System.Diagnostics.Process.Start("google chrome.lnk"); break; case "jarvis time": speaker.Speak("Sir , The Time is " + DateTime.Now.ToString("h:mm tt")); break; case "jarvis date": speaker.Speak("Sir , The Date is " + DateTime.Now.Date); break; case "jarvis day": speaker.Speak("Sir, The Day is " + DateTime.Now.DayOfWeek); break; case "thank you": speaker.Speak("You'r welcome sir"); break; default: break; } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д