unity实现语音识别:PC端、移动端
一、PC端(方式一)
Unity有个自带的库,使用它可以很方便的实现PC端的语音识别;可以用它来识别一些关键字,进行短语句的识别。
“UnityEngine.Windows.Speech”这个库需要一个依赖文件:点击跳转
**提取码:xqb9 **
引入命名空间:
using UnityEngine.Windows.Speech;
设置识别可信度
public ConfidenceLevel mConfidenceLevel = ConfidenceLevel.Medium;
要识别的关键字
public string[] keyWords = { "哈哈", "你是谁", "不可以" };
声明一个语音识别器
mPhraseRecognizer = new KeywordRecognizer(keyWords, mConfidenceLevel);
脚本代码如下↓:
using UnityEngine;
using UnityEngine.Windows.Speech;//引入命名空间
/// <summary>
/// 语音识别(主要是别关键字)
/// </summary>
public class MyYuYin : MonoBehaviour
{
// 短语识别器
PhraseRecognizer mPhraseRecognizer;
// 关键字
public string[] keyWords = { "哈哈", "你是谁", "不可以" };
// 可信度
public ConfidenceLevel mConfidenceLevel = ConfidenceLevel.Medium;
void Start()
{
//开始时就创建
if (mPhraseRecognizer == null)
{
//创建一个识别器
mPhraseRecognizer = new KeywordRecognizer(keyWords, mConfidenceLevel);
//注册监听方法
mPhraseRecognizer.OnPhraseRecognized += M_RecognitionListener;
//语音识别器开启
mPhraseRecognizer.Start();
Debug.Log("创建识别器成功");
}
}
/// <summary>
/// 识别器的监听方法,当时别到声明的关键字时的回调函数
/// </summary>
/// <param name="args"></param>
private void M_RecognitionListener(PhraseRecognizedEventArgs args)
{
//打印识别到的文本
Debug.Log(args.text);
}
/// <summary>
/// 销毁脚本时,释放语音识别器
/// </summary>
private void OnDestroy()
{
//判断是否有语音识别器,如果有,则释放
if (mPhraseRecognizer != null)
{
//用完应该释放,否则会带来额外的开销
mPhraseRecognizer.Dispose();
}
}
}
二、PC端(方式二)
上面记录了用unity自带的方式实现pc的语音识别,还有很多方式,这两天看了一下用讯飞语音来实现;感觉还是蛮准的。
三、移动端(安卓)
可以用讯飞和百度AI实现