I've been trying to write this speech recognition app for a college project. I am learning C# on my self and I'm pretty new to it. I got this error when i tried to build the application, I got the following error:
System.UnauthorizedAccessException HResult=0x80070005
Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Source=System.Speech StackTrace: at System.Speech.Internal.SapiInterop.ISpRecognizer.GetFormat(SPSTREAMFORMATTYPE WaveFormatType, Guid& pFormatId, IntPtr& ppCoMemWFEX) at System.Speech.Internal.SapiInterop.SapiRecognizer.<>c__DisplayClass13_0.b__0() at System.Speech.Internal.SapiInterop.SapiProxy.PassThrough.Invoke(ObjectDelegate pfn) at System.Speech.Internal.SapiInterop.SapiRecognizer.GetFormat(SPSTREAMFORMATTYPE WaveFormatType) at System.Speech.Recognition.RecognizerBase.GetSapiAudioFormat() at System.Speech.Recognition.RecognizerBase.UpdateAudioFormat(SpeechAudioFormatInfo audioFormat) at System.Speech.Recognition.RecognizerBase.SetInputToDefaultAudioDevice() at System.Speech.Recognition.SpeechRecognitionEngine.SetInputToDefaultAudioDevice() at RailVoice.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Soorya S Rajan\source\repos\RailVoice\RailVoice\Form1.cs:line 35 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
My C# application has only one form in it and the code has been attached below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
namespace RailVoice
{
public partial class Form1 : Form
{
SpeechRecognitionEngine speechRecognitionEngine = new SpeechRecognitionEngine();
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
SpeechRecognitionEngine speechListener = new SpeechRecognitionEngine();
Random random = new Random();
int voiceTimeOut = 0;
DateTime time = DateTime.Now;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
speechRecognitionEngine.SetInputToDefaultAudioDevice();
speechRecognitionEngine.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Commands.txt")))));
speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Default_SpeechRecognized);
speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
speechListener.SetInputToDefaultAudioDevice();
speechRecognitionEngine.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Commands.txt")))));
speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(startlistening_SpeechRecognized);
}
private void Default_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
int randNum;
string speech = e.Result.Text;
Console.WriteLine(speech);
if (speech == "Hello")
{
speechSynthesizer.SpeakAsync("Hey User");
}
if(speech == "Bye")
{
speechSynthesizer.SpeakAsync("Byeee user");
}
if(speech == "What is the date")
{
speechSynthesizer.SpeakAsync(DateTime.Now.ToString("h mm tt"));
}
if(speech == "Stop talking")
{
speechSynthesizer.SpeakAsyncCancelAll();
randNum = random.Next(1, 3);
if(randNum == 1)
{
speechSynthesizer.SpeakAsync("Okay");
}
else if(randNum == 2)
{
speechSynthesizer.SpeakAsync("Okay sir");
}
else if(randNum == 3)
{
speechSynthesizer.SpeakAsync("I will stay quiet");
}
}
if(speech == "Stop listening")
{
speechSynthesizer.SpeakAsync("Call me any time you want");
speechRecognitionEngine.RecognizeAsyncCancel();
speechListener.RecognizeAsync(RecognizeMode.Multiple);
}
if(speech == "Show commands")
{
string[] MyCommands = File.ReadAllLines(@"Commands");
listBox1.Items.Clear();
listBox1.SelectionMode = SelectionMode.None;
listBox1.Visible = true;
foreach(string commands in MyCommands)
{
listBox1.Items.Add(commands);
}
}
if(speech == "Hide commands")
{
listBox1.Visible = false;
}
}
private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
voiceTimeOut = 0;
}
private void startlistening_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
if(speech == "Hello assistant")
{
speechListener.RecognizeAsyncCancel();
speechSynthesizer.SpeakAsync("Hello, what do you want");
speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if(voiceTimeOut == 10)
{
speechRecognitionEngine.RecognizeAsyncCancel();
}
else if(voiceTimeOut == 1)
{
speechTimer.Stop();
speechListener.RecognizeAsync();
voiceTimeOut = 0;
}
}
}
}
I tried googling but I couldn't find any suitable results. If anyone knows what this error is, please do drop a message. Thanks in advance :)