Users can register and listen for hypothesis and phrase completed events. Start() and Stop() methods respectively enable and disable dictation recognition. Once done with the recognizer, it must be disposed using Dispose() method to release the resources it uses. It will release these resources automatically during garbage collection at an additional performance cost if they are not released prior to that.
#pragma strict
public class DictationScript extends MonoBehaviour {
@SerializeField
private var m_Hypotheses: Text;
@SerializeField
private var m_Recognitions: Text;
private var m_DictationRecognizer: DictationRecognizer;
function Start() {
m_DictationRecognizer = new DictationRecognizer();
m_DictationRecognizer.DictationResult += function(text, confidence) {
Debug.LogFormat("Dictation result: {0}", text);
m_Recognitions.text += text + "\n";
};
m_DictationRecognizer.DictationHypothesis += function(text) {
Debug.LogFormat("Dictation hypothesis: {0}", text);
m_Hypotheses.text += text + Env;
};
m_DictationRecognizer.DictationComplete += function(completionCause) {
if (cause != DictationCompletionCause.Complete)
Debug.LogErrorFormat("Dictation completed unsuccessfully: {0}.", cause);
};
m_DictationRecognizer.DictationError += function(error, hresult) {
Debug.LogErrorFormat("Dictation error: {0}; HResult = {1}.", error, hresult);
};
m_DictationRecognizer.Start();
}
}
public class DictationScript : MonoBehaviour
{
[SerializeField]
private Text m_Hypotheses;
[SerializeField]
private Text m_Recognitions;
private DictationRecognizer m_DictationRecognizer;
void Start()
{
m_DictationRecognizer = new DictationRecognizer();
m_DictationRecognizer.DictationResult += (text, confidence) =>
{
Debug.LogFormat("Dictation result: {0}", text);
m_Recognitions.text += text + "\n";
};
m_DictationRecognizer.DictationHypothesis += (text) =>
{
Debug.LogFormat("Dictation hypothesis: {0}", text);
m_Hypotheses.text += text + Env;
};
m_DictationRecognizer.DictationComplete += (completionCause) =>
{
if (cause != DictationCompletionCause.Complete)
Debug.LogErrorFormat("Dictation completed unsuccessfully: {0}.", cause);
};
m_DictationRecognizer.DictationError += (error, hresult) =>
{
Debug.LogErrorFormat("Dictation error: {0}; HResult = {1}.", error, hresult);
};
m_DictationRecognizer.Start();
}
}
Dictation recognizer is currently functional only on Windows 10, and requires that dictation is permitted in the user's Speech privacy policy (Settings->Privacy->Speech, inking & typing). If dictation is not enabled, DictationRecognizer will fail on Start. Developers can handle this failure in an app-specific way by providing a DictationError delegate and testing for SPERR_SPEECH_PRIVACY_POLICY_NOT_ACCEPTED (0x80045509).