DictationRecognizer 监听语音输入,并尝试确定说出的短语。
用户可以注册和监听假设和短语完成事件。Start() 和 Stop() 方法分别用于启用和禁用听写识别。用完识别器后,必须使用 Dispose() 方法对它进行处置以释放它使用的资源。它将在垃圾收集期间自动释放这些资源,如果资源没有在这之前得到释放,将会产生额外的性能成本。
using UnityEditor; using UnityEngine; using UnityEngine.UI; using UnityEngine.Windows.Speech;
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; };
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(); } }
听写识别器目前仅在 Windows 10 上可用,并且要求在用户的语音隐私政策(Settings > Privacy > Speech,inking & typing)中允许听写。如果听写未启用,DictationRecognizer 将会在 Start 上失败。开发者可以通过提供 DictationError 委托和测试 SPERR_SPEECH_PRIVACY_POLICY_NOT_ACCEPTED (0x80045509),以特定于此应用程序的方式处理此故障。
AutoSilenceTimeoutSeconds | 由于缺乏音频输入,听写识别器会话结束前持续的时长(以秒为单位)。 |
InitialSilenceTimeoutSeconds | 由于缺乏音频输入,导致不能在当前会话中听过任何音频,听写识别器会话在结束之前持续的时长(以秒为单位) |
Status | 指示听写识别器的状态。 |
DictationRecognizer | 使用指定的最小可信度和听写主题约束创建 DictationRecognizer。位于指定最低水平下的短语将被忽略。 |
DictationComplete | 当识别器会话结束时触发的事件。 |
DictationError | 当识别器会话发生错误时触发的事件。 |
DictationHypothesis | 当识别器更改它对当前片元的假设时触发的事件。 |
DictationResult | 指示已采用指定可信度识别某个短语的事件。 |
DictationCompletedDelegate | DictationComplete 事件的委托。 |
DictationErrorHandler | DictationError 事件的委托。 |
DictationHypothesisDelegate | 指示假设更改事件的回调。您应注册 DictationHypothesis 事件。 |
DictationResultDelegate | 指示已采用指定可信度识别某个短语的回调。您应注册 DictationResult 事件。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.