Search Unity

Trying to use Windows speech API in Unity

Discussion in 'Scripting' started by bluesyhfang, Jun 28, 2015.

  1. bluesyhfang

    bluesyhfang

    Joined:
    Apr 4, 2014
    Posts:
    8
    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 `EventHandler`1' could not be found. Are you missing a using directive or an assembly reference?"

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6.  
    7. using System.Speech.AudioFormat;
    8. using System.Speech.Recognition;
    9. using System.Speech.Recognition.SrgsGrammar;
    10. using System.Speech.Synthesis;
    11. using System.Speech.Synthesis.TtsEngine;
    12.  
    13. public class VoiceRecognition : MonoBehaviour {
    14.  
    15.     SpeechSynthesizer synth;
    16.     SpeechRecognitionEngine speechRecognitionEngine;
    17.     System.Timers.Timer timer;
    18.    
    19.     // Use this for initialization
    20.     void Start () {
    21.         // the recognition engine
    22.         speechRecognitionEngine = new SpeechRecognitionEngine();
    23.  
    24.         speechRecognitionEngine.AudioLevelUpdated +=
    25.             new EventHandler<AudioLevelUpdatedEventArgs>(engine_AudioLevelUpdated);
    26.         speechRecognitionEngine.SpeechRecognized +=
    27.             new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
    28.         speechRecognitionEngine.SpeechDetected +=
    29.             new EventHandler<SpeechDetectedEventArgs>(sre_SpeechDetected);
    30.         speechRecognitionEngine.SpeechHypothesized +=
    31.             new EventHandler<SpeechHypothesizedEventArgs>(sre_SpeechHypothesized);
    32.         speechRecognitionEngine.SpeechRecognitionRejected +=
    33.             new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRejected);
    34.  
    35.         Choices colors = new Choices();
    36.         colors.Add(new string[] { "front", "back", "up", "down", "left", "right" });
    37.         GrammarBuilder gb = new GrammarBuilder();
    38.         gb.Append(colors);
    39.         Grammar g = new Grammar(gb);
    40.         //Grammar g = new DictationGrammar();
    41.         speechRecognitionEngine.LoadGrammar(g);
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update () {
    46.        
    47.     }
    48.     // Create a simple handler for the SpeechRecognized event.
    49.     void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    50.     {
    51.         //Console.WriteLine(e.Result.Text);
    52.         //MessageBox.Show("Speech recognized: " + e.Result.Text);
    53.         Debug.Log("Speech recognized: " + e.Result.Text + "\r\n");
    54.     }
    55.    
    56.     void engine_AudioLevelUpdated(object sender, AudioLevelUpdatedEventArgs e)
    57.     {
    58.         Debug.Log("Audio detected: " + e.AudioLevel + "\r\n");
    59.     }
    60.  
    61. }
    62.  
    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.
     
  2. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Code (csharp):
    1. using System;
    I think ;)
     
  3. bluesyhfang

    bluesyhfang

    Joined:
    Apr 4, 2014
    Posts:
    8
    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 <filename unknown>:0

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

    at System.Reflection.Assembly.GetCustomAttributes (System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0
     
  4. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    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.
     
  5. catron2

    catron2

    Joined:
    Jan 29, 2016
    Posts:
    1
    These APIs are supported on all windows platforms as long as they're running on Windows 10: Windows Editor, Windows Standalone and Windows Store