Trying to use Windows speech API in Unity

Hello, I am trying to use Window speech API in Unity. I copied the System.Speech.dll to the asset folder and use the code below. However, I got the following error and it seems I need to use something else to replace the event handler. Any suggestions?

“Assets/VoiceRecognition.cs(29,29): error CS0246: The type or namespace name EventHandler1’ could not be found. Are you missing a using directive or an assembly reference?”

using UnityEngine;
using System.Collections;

using System.Collections.Generic;
using System.ComponentModel;

using System.Speech.AudioFormat;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
using System.Speech.Synthesis;
using System.Speech.Synthesis.TtsEngine;

public class VoiceRecognition : MonoBehaviour {

    SpeechSynthesizer synth;
    SpeechRecognitionEngine speechRecognitionEngine;
    System.Timers.Timer timer;
   
    // Use this for initialization
    void Start () {
        // the recognition engine
        speechRecognitionEngine = new SpeechRecognitionEngine();

        speechRecognitionEngine.AudioLevelUpdated +=
            new EventHandler<AudioLevelUpdatedEventArgs>(engine_AudioLevelUpdated);
        speechRecognitionEngine.SpeechRecognized +=
            new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
        speechRecognitionEngine.SpeechDetected +=
            new EventHandler<SpeechDetectedEventArgs>(sre_SpeechDetected);
        speechRecognitionEngine.SpeechHypothesized +=
            new EventHandler<SpeechHypothesizedEventArgs>(sre_SpeechHypothesized);
        speechRecognitionEngine.SpeechRecognitionRejected +=
            new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRejected);

        Choices colors = new Choices();
        colors.Add(new string[] { "front", "back", "up", "down", "left", "right" });
        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(colors);
        Grammar g = new Grammar(gb);
        //Grammar g = new DictationGrammar();
        speechRecognitionEngine.LoadGrammar(g);
    }
   
    // Update is called once per frame
    void Update () {
       
    }
    // Create a simple handler for the SpeechRecognized event.
    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        //Console.WriteLine(e.Result.Text);
        //MessageBox.Show("Speech recognized: " + e.Result.Text);
        Debug.Log("Speech recognized: " + e.Result.Text + "\r\n");
    }
   
    void engine_AudioLevelUpdated(object sender, AudioLevelUpdatedEventArgs e)
    {
        Debug.Log("Audio detected: " + e.AudioLevel + "\r\n");
    }

}

Hello, I am trying to use Windows speech API in Unity. I copied the System.Speech.dll to the asset folder and below is my code.

using System;

I think :wink:

Thanks Jamster. The EventHandler problem is solved!

However, I got another even trickier problem. The console says as follows:

Internal compiler error. See the console log for more information. output was: Unhandled Exception: System.TypeLoadException: Could not load type ‘System.Runtime.CompilerServices.ReferenceAssemblyAttribute’ from assembly ‘System.Speech’.

at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType) [0x00000] in :0

at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00000] in :0

at System.Reflection.Assembly.GetCustomAttributes (System.Type attributeType, Boolean inherit) [0x00000] in :0

I know this post is old but I am writing this to help other people.

As of Unity Beta 5.4.0B2, Windows speech API is now implemented. All you have to do is include Unity Speech API with namespace using UnityEngine.Windows.Speech and you are now ready to use the Windows speech API.

You should expect it to be fully functional with bug fixes if there is any with Unity 5.4 release. Although the Beta release work fine.

1 Like

These APIs are supported on all windows platforms as long as they’re running on Windows 10: Windows Editor, Windows Standalone and Windows Store