Unity开发之实现简单的关键字识别

Unity开发文档

1.引用

UnityEngine.Windows.Speech中的类

2.功能介绍

关键词识别器监听语音输入,并尝试将说出的短语与注册的关键词列表进行匹配。
在任何给定的时间都可以有许多活跃的关键字识别器,但是没有两个关键字识别器可以监听同一个关键字。

3.官方文档的Dome
using System;
using System.Text;
using UnityEngine;
using UnityEngine.Windows.Speech;

public class KeywordScript : MonoBehaviour
{
    [SerializeField]
    private string[] m_Keywords;

    private KeywordRecognizer m_Recognizer;

    void Start()
    {
        m_Recognizer = new KeywordRecognizer(m_Keywords);
        m_Recognizer.OnPhraseRecognized += OnPhraseRecognized;
        m_Recognizer.Start();
    }

    private void OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendFormat("{0} ({1}){2}", args.text, args.confidence, Environment.NewLine);
        builder.AppendFormat("\tTimestamp: {0}{1}", args.phraseStartTime, Environment.NewLine);
        builder.AppendFormat("\tDuration: {0} seconds{1}", args.phraseDuration.TotalSeconds, Environment.NewLine);
        Debug.Log(builder.ToString());
    }
}
4.字段
5.方法

① KeywordRecognizer方法

  Create a KeywordRecognizer which listens to specified keywords with the specified minimum confidence. Phrases under the specified minimum level will be ignored.
  翻译: 创建一个关键字识别器,它以指定的最小置信度侦听指定的关键字。低于指定最低级别的短语将被忽略。如果没有指定最小置信水平,则置信水平被假定为中等。
  • 方法重载

    public KeywordRecognizer(string[] keywords);
    public KeywordRecognizer(string[] keywords, Windows.Speech.ConfidenceLevel minimumConfidence);
    
    • 参数1:keywords

      The keywords that the recognizer will listen to.
      翻译: 识别器将侦听的关键字。
      
    • 参数2:minimumConfidence

      The minimum confidence level of speech recognition that the recognizer will accept.
      翻译: 识别器将接受的语音识别的最小置信度。
      

② IsRunning

Tells whether the phrase recognizer is listening for phrases.
翻译: 告诉短语识别器是否正在侦听短语。
  • public bool IsRunning ()

③ Dispose

Disposes the resources used by phrase recognizer.
翻译: 释放短语识别器使用的资源。

Start

Makes the phrase recognizer start listening to phrases.
翻译: 使短语识别器开始听短语。
  • 如果启动短语识别器时停止PhraseRecognitionSystem,它将重新启动。
  • public void Start ()

Stop

Stops the phrase recognizer from listening to phrases.
翻译: 停止短语识别器收听短语。
  • public void Stop ()

OnPhraseRecognized

Event that gets fired when the phrase recognizer recognizes a phrase.
翻译: 当短语识别器识别短语时触发的事件。

PhraseRecognizedDelegate

Delegate for OnPhraseRecognized event
翻译: OnPhraseRecognized事件的委托。
5.简单的使用
using System;
using System.Text;
using UnityEngine;
using UnityEngine.Windows.Speech;
public class text : MonoBehaviour {
    [SerializeField]
    private string[] m_Keywords = {"一","二","三","四","五","六","七","八","九","十" };//关键字
    //关键字识别器
    private KeywordRecognizer m_Recognizer;
    // Use this for initialization
    void Start () {
        //创建一个关键字识别器
        m_Recognizer = new KeywordRecognizer(m_Keywords);
        Debug.Log("创建识别器成功");
        m_Recognizer.OnPhraseRecognized += OnPhraseRecognized;
        
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            m_Recognizer.Start();
            Debug.Log("开始识别");
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            m_Recognizer.Stop();
            Debug.Log("识别结束");
        }
    }
    //当短语识别器识别短语时触发的事件。
    private void OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendFormat("{0}", args.text);
        Debug.Log(builder.ToString());
    }
}

源码下载

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值