核心定义包附带 DictationSubsystem,是 和 IDictationsystem 的基本MRTKSubsystem实现,用作 MRTK3 中负责听写的子系统的基础。 作为 MRTK 的一部分提供的具体实现,例如 WindowsDictationSubsystem ,你可能生成的其他潜在听写子系统应都基于此类。 继承自 DictationSubsystem 的子系统允许启动和停止听写会话,并提供在处理语音输入、识别最终结果以及听写会话完成或出错时触发的事件。 可以将事件处理程序添加到这些事件中,以处理听写功能。
DictationSubsystem dictationSubsystem = XRSubsystemHelpers.GetFirstRunningSubsystem<DictationSubsystem>();
if (dictationSubsystem != null)
{
dictationSubsystem.Recognizing += DictationSubsystem_Recognizing;
dictationSubsystem.Recognized += DictationSubsystem_Recognized;
dictationSubsystem.RecognitionFinished += DictationSubsystem_RecognitionFinished;
dictationSubsystem.RecognitionFaulted += DictationSubsystem_RecognitionFaulted;
dictationSubsystem.StartDictation();
}
在触发事件时,你将使用任何想要发生的事件来实现这些事件处理程序。
完成后,删除这些事件处理程序。
if (dictationSubsystem != null)
{
dictationSubsystem.StopDictation();
dictationSubsystem.Recognizing -= DictationSubsystem_Recognizing;
dictationSubsystem.Recognized -= DictationSubsystem_Recognized;
dictationSubsystem.RecognitionFinished -= DictationSubsystem_RecognitionFinished;
dictationSubsystem.RecognitionFaulted -= DictationSubsystem_RecognitionFaulted;
}
完整识别程序代码:
using MixedReality.Toolkit;
using MixedReality.Toolkit.Subsystems;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 显示如何订阅和处理 DictationSubsystem 触发事件的演示脚本。
/// </summary>
public class DictationHandler : MonoBehaviour
{
[System.Serializable]
public class StringUnityEvent : UnityEvent<string> { }
/// <summary>
/// 当用户说话时引发的事件。当识别程序侦听时,它提供到目前为止所听到的内容的文本。
/// </summary>
[field: SerializeField]
public StringUnityEvent OnSpeechRecognizing { get; private set; }
/// <summary>
/// 事件在用户停顿后引发,通常在句末。包含目前为止完整识别的字符串。
/// </summary>
[field: SerializeField]
public StringUnityEvent OnSpeechRecognized { get; private set; }
/// <summary>
/// 事件在识别程序停止时引发。包含最终识别的字符串。
/// </summary>
[field: SerializeField]
public StringUnityEvent OnRecognitionFinished { get; private set; }
/// <summary>
/// 事件。包含错误原因的字符串表示形式。
/// </summary>
[field: SerializeField]
public StringUnityEvent OnRecognitionFaulted { get; private set; }
private DictationSubsystem dictationSubsystem;
/// <summary>
/// 在 DictationSubsystem 上开始听写.
/// </summary>
public void StartRecognition()
{
// 确保没有正在进行的听写
StopRecognition();
dictationSubsystem = XRSubsystemHelpers.GetFirstRunningSubsystem<DictationSubsystem>();
if (dictationSubsystem != null)
{
dictationSubsystem.Recognizing += DictationSubsystem_Recognizing;
dictationSubsystem.Recognized += DictationSubsystem_Recognized;
dictationSubsystem.RecognitionFinished += DictationSubsystem_RecognitionFinished;
dictationSubsystem.RecognitionFaulted += DictationSubsystem_RecognitionFaulted;
dictationSubsystem.StartDictation();
}
else
{
OnRecognitionFaulted.Invoke("找不到正在运行的 DictationSubsystem。请检查 MRTK 配置文件设置 " +
"(Project Settings -> MRTK3) 或确保 DictationSubsystem 正在运行.");
}
}
private void DictationSubsystem_RecognitionFaulted(DictationSessionEventArgs obj)
{
OnRecognitionFaulted.Invoke("识别错误,原因: " + obj.ReasonString);
}
private void DictationSubsystem_RecognitionFinished(DictationSessionEventArgs obj)
{
OnRecognitionFinished.Invoke("识别结束,原因: " + obj.ReasonString);
}
private void DictationSubsystem_Recognized(DictationResultEventArgs obj)
{
OnSpeechRecognized.Invoke("Recognized:" + obj.Result);
}
private void DictationSubsystem_Recognizing(DictationResultEventArgs obj)
{
OnSpeechRecognizing.Invoke("Recognizing:" + obj.Result);
}
/// <summary>
/// 停止当前 DictationSubsystem 上的听写。
/// </summary>
public void StopRecognition()
{
if (dictationSubsystem != null)
{
dictationSubsystem.StopDictation();
dictationSubsystem.Recognizing -= DictationSubsystem_Recognizing;
dictationSubsystem.Recognized -= DictationSubsystem_Recognized;
dictationSubsystem.RecognitionFinished -= DictationSubsystem_RecognitionFinished;
dictationSubsystem.RecognitionFaulted -= DictationSubsystem_RecognitionFaulted;
dictationSubsystem = null;
}
}
}