启用麦克风
Project Settings -> Player-> Project Settings -> Player
关键字识别
- Step 1: 定义字符串数组
- Step 2: 订阅和处理OnPhraseRecognized事件。当给定短语被识别时,将调用此事件。.
- Step 3: 开始关键字识别
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Windows.Speech;
public class KeyWordRecognizerBehaviour : MonoBehaviour {
KeywordRecognizer keywordRecognizer;
// keyword array
public string[] Keywords_array;
// Use this for initialization
void Start () {
// Change size of array for your requirement
Keywords_array = new string[2];
Keywords_array [0] = "hello";
Keywords_array [1] = "how are you";
// instantiate keyword recognizer, pass keyword array in the constructor
keywordRecognizer = new KeywordRecognizer(Keywords_array);
keywordRecognizer.OnPhraseRecognized += OnKeywordsRecognized;
// start keyword recognizer
keywordRecognizer.Start ();
}
void OnKeywordsRecognized(PhraseRecognizedEventArgs args)
{
Debug.Log ("Keyword: " + args.text + "; Confidence: " + args.confidence + "; Start Time: " + args.phraseStartTime + "; Duration: " + args.phraseDuration);
// write your logic
}
}
如果在项目中使用多个关键字识别器,可以使用PhraseRecognitionSystem类用一行代码停止并重新启动所有关键字识别。
// shut down phrase recognition system. All keyword recognizer will be stopped
PhraseRecognitionSystem.Shutdown();
// restore all recognizer in the previous state
PhraseRecognitionSystem.Restart();