之前的博文有提到使用windows自带的语音识别,我们项目在每一个场景中都加入了语音识别,但是当遇到场景跳转时,到达第二个场景后,语音识别的开启会概率性出错,尝试多种方法后进行暴力方法解决此错误。
暴力解决方案如下:
当语音识别出现超时错误后判断错误方式,重新实例化语音识别,并开启;当语音识别过程中出现其他错误后,也重新实例化语音识别并开启,在经过多次测试后发现此方比较稳定,出错率大大降低。
当发生场景跳转时,Dispose方法注销方法时或停止实例化的语音识别后会偶尔出现错误但不影响程序的运行,具体原因暂不清楚。
系统中选择单元场景的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Windows.Speech;
using System;
using System.Text;
using System.IO;
using NAudio.Wave;
using System;
public class unit : MonoBehaviour
{
private string strFilname;
public AudioSource Source;
[Tooltip("A text area for the recognizer to display the recognized strings.")]
public Text DictationDisplay;
private DictationRecognizer dictationRecognizer;
bool mark = true;
public IEnumerator LoadMusic(string filepath, string savepath)
{
var stream = File.Open(filepath, FileMode.Open);
var reader = new Mp3FileReader(stream);
WaveFileWriter.CreateWaveFile(savepath, reader);
var www = new WWW("file://" + savepath);
yield return www;
var clip = www.GetAudioClip();
Source.clip = clip;
Source.Play();
}
// Use this for initialization
void Start()
{
Debug

在Unity3D项目中使用Windows内置语音识别功能时,场景跳转可能导致语音识别错误。为了解决这个问题,采取了暴力解决策略:当检测到语音识别超时或出现其他错误时,立即重新实例化并开启语音识别。这种方法经过测试,提高了系统的稳定性,降低了出错率。虽然在场景切换时偶尔会有错误,但不会影响程序整体运行。代码中展示了如何在场景切换时选择和管理语音识别实例。
最低0.47元/天 解锁文章





