Monday, 14 January 2019

Creating a Dictation Recognizer in Unity C# - Hololens

1.Create a new scene and apply the mixed reality scene settings.
2. Create a UI text component and drag on to the inputtext field on the next step after attaching the code.
2. Create a new button and attach the below script and trigger the InitateRecording function on click event


using UnityEditor;

using UnityEngine.UI;
using UnityEngine;
using HoloToolkit.Unity.InputModule;
using UnityEngine.Windows.Speech;

public class DictationHandler : MonoBehaviour {

   
    public GameObject inputtext = null, icon = null;
    public Material recording = null, stopped = null;
 
         [SerializeField]
    private Text m_Hypotheses;

    [SerializeField]
    private Text m_Recognitions;

    private DictationRecognizer m_DictationRecognizer;

    bool isRecording;

    public void InitateRecording()
    {
        if(!isRecording)
        {
            isRecording = true;
            StartRecording();
        }
        else
        {
            isRecording = false;
            StopRecording();
        }
    }

     void StartRecording()
    {
        icon.GetComponent<MeshRenderer>().material = recording;
        m_DictationRecognizer = new DictationRecognizer();

        m_DictationRecognizer.DictationResult += (text, confidence) =>
        {
            Debug.LogFormat("Dictation result: {0}", text);
            inputtext.GetComponent<UnityEngine.UI.Text>().text += text ;
        };

        m_DictationRecognizer.DictationHypothesis += (text) =>
        {
            Debug.LogFormat("Dictation hypothesis: {0}", text);
            m_Hypotheses.text += text;
        };

        m_DictationRecognizer.DictationComplete += (completionCause) =>
        {
            if (completionCause != DictationCompletionCause.Complete)
                Debug.LogErrorFormat("Dictation completed unsuccessfully: {0}.", completionCause);
        };

        m_DictationRecognizer.DictationError += (error, hresult) =>
        {
            Debug.LogErrorFormat("Dictation error: {0}; HResult = {1}.", error, hresult);
        };

        m_DictationRecognizer.Start();
    }
     void StopRecording()
    {
        m_DictationRecognizer.Stop();
        icon.GetComponent<MeshRenderer>().material = stopped;
    }

    }

No comments:

Post a Comment