1. Window 自带的微软语音识别
参考官方手册:Windows.Speech.DictationRecognizer - Unity 脚本 API (unity3d.com)
这个中英识别都比较准确,不过应该只支持Win10以上。
需在设置里开启在线语音识别。
代码部分:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using UnityEngine.Windows.Speech; public class DictationScript : MonoBehaviour { public Text hypothesesText;//显示输入过程中猜想结果的 public Text recognitionsText;//显示识别结果的 public Button startBtn; public Button stopBtn; private DictationRecognizer dictationRecognizer; void Start() { dictationRecognizer = new DictationRecognizer(); dictationRecognizer.DictationResult += OnDictationResult; dictationRecognizer.DictationHypothesis += OnDictationHypothesis; dictationRecognizer.DictationComplete += OnDictationComplete; dictationRecognizer.DictationError += OnDictationError; //dictationRecognizer.Start(); startBtn.onClick.AddListener(DictationStart); stopBtn.onClick.AddListener(DictionStop); } private void OnDestroy() { dictationRecognizer.Stop(); dictationRecognizer.Dispose(); } ////// 语音识别结果 /// /// 识别结果 /// private void OnDictationResult(string text, ConfidenceLevel confidence) { Debug.LogFormat("识别结果: {0}", text); recognitionsText.text = text; DictionStop();//我是希望得到结果就自动停止输入 } ////// 语音输入过程中对结果猜想时触发的事件。 /// /// 识别猜想 private void OnDictationHypothesis(string text) { Debug.LogFormat("识别猜想: {0}", text); hypothesesText.text = text; } private void OnDictationComplete(DictationCompletionCause cause) { if (cause != DictationCompletionCause.Complete) Debug.LogErrorFormat("识别失败: {0}.", cause); } private void OnDictationError(string error, int hresult) { Debug.LogErrorFormat("识别错误: {0}; HResult = {1}.", error, hresult); } ////// 开启听写识别会话 /// public void DictationStart() { dictationRecognizer.Start(); startBtn.gameObject.SetActive(false); stopBtn.gameObject.SetActive(true); } ////// 结束听写识别会话 /// public void DictionStop() { dictationRecognizer.Stop(); startBtn.gameObject.SetActive(true); stopBtn.gameObject.SetActive(false); } }
2. 百度SDK
也可以考虑其他平台的SDK,我只测试了百度的,这个英文识别不大好(最起码Unity识别不出来,IPad可以识别出来),上面那个中英识别都不错,不过只支持Window,百度的这个我也没测试其他平台,自行测试吧。
官方文档:https://ai.baidu.com/ai-doc/SPEECH/ilbxfvpau
- 在百度智能云控制台创建一个应用,勾上短语音识别
- 下载C# SDK:https://ai.baidu.com/sdk#asr
- 将SDK 放入Unity中Plugins文件夹下
然后直接上代码吧
using System.Collections; using System.Collections.Generic; using UnityEngine; using Baidu.Aip.Speech; using UnityEngine.UI; public class BaiduSpeech : MonoBehaviour { public Text text;//显示结果用的 // 设置APPID/AK/SK string APP_ID = "你的 AppID"; string API_KEY = "你的 API Key"; string SECRET_KEY = "你的 Secret Key"; int rate = 8000; Asr client; AudioSource aud; int audioLength;//录音的长度 private void Awake() { if (GetComponent() == null) aud = gameObject.AddComponent(); else aud = gameObject.GetComponent(); aud.playOnAwake = false; } // Start is called before the first frame update void Start() { client = new Asr(APP_ID, API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 } ////// 开始录音 /// public void StartMic() { if (Microphone.devices.Length == 0) return; Microphone.End(null); Debug.Log("Start"); aud.clip = Microphone.Start(null, false, 10, rate); } ////// 结束录音 /// public void EndMic() { int lastPos = Microphone.GetPosition(null); if (Microphone.IsRecording(null)) audioLength = lastPos / rate;//录音时长 else audioLength = 10; Debug.Log("Stop"); Microphone.End(null); AsrData(GetClipData()); } ////// 把录音转换为Byte[] /// ///public byte[] GetClipData() { if (aud.clip == null) { Debug.LogError("录音数据为空"); return null; } float[] samples = new float[aud.clip.samples]; aud.clip.GetData(samples, 0); byte[] outData = new byte[samples.Length * 2]; int rescaleFactor = 32767; //to convert float to Int16 for (int i = 0; i < samples.Length; i++) { short temshort = (short)(samples[i] * rescaleFactor); byte[] temdata = System.BitConverter.GetBytes(temshort); outData[i * 2] = temdata[0]; outData[i * 2 + 1] = temdata[1]; } if (outData == null || outData.Length <= 0) { Debug.LogError("录音数据为空"); return null; } return outData; } // 识别本地文件 public void AsrData(byte[] data) { // 可选参数 var options = new Dictionary { {"dev_pid", 1537} }; client.Timeout = 120000; // 若语音较长,建议设置更大的超时时间. ms var result = client.Recognize(data, "pcm", rate, options); if (result["result"] != null) { text.text = result["result"][0].ToString(); } else { Debug.Log("识别错误"); } Debug.Log(result); } }
猜你喜欢
- 7天前(a级景区评定机构)全国A级旅游景区创建与提升培训班在敦煌市举办
- 7天前(鄂尔多斯航空公司客服电话)架起“北方之路” ,中国联合航空带您飞向鄂尔多斯重回1倍速
- 7天前(三亚海棠湾君悦度假酒店)三亚海棠湾君悦酒店暑期夏令营悦趣海岛游招募中
- 7天前(安徽民宿发展报告)首届安徽省乡村民宿创意设计大赛启动
- 7天前(中旅酒店 维景)中旅酒店首次AI数字人直播亮相南京维景
- 7天前(曼谷丽思卡尔顿公寓价格)曼谷丽思卡尔顿酒店盛大启幕,开创泰国奢华雅致新纪元
- 7天前(纳米比亚旅游报价)纳米比亚旅游局2024年中国推介会圆满落幕
- 7天前(安岚度假村及酒店推出"山海之约"目的地婚礼计划)安岚度假村及酒店推出"山海之约"目的地婚礼计划
- 7天前(天津四季酒店开业时间)天津四季酒店邀你开启灿烂暑假
- 7天前(辽宁新增6个国家4a级旅游景区有哪些)辽宁新增6个国家4A级旅游景区
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章