通过修改ExampleAssistant.cs
和ExampleStreaming.cs
脚本,一旦识别结果为final
,我便将消息/讲话发送到Assistant服务,以便在Unity中进行对话,就好像您在键入在聊天框中。
这意味着我必须在Active
脚本的false
事件处理方法中将OnRecognize
属性设置为ExampleStreaming.cs
。
但是,一旦发送了语音并且我将其设置回true
以继续收听/记录并识别用户的语音,该服务就不会再次启动。
使用调试消息,我已经确认该属性确实设置回true
并且执行了StartRecording
方法。
private void OnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData)
{
if (result != null && result.results.Length > 0)
{
foreach (var res in result.results)
{
foreach (var alt in res.alternatives)
{
string text = string.Format("{0} ({1}, {2:0.00})\n", alt.transcript, res.final ? "Final" : "Interim", alt.confidence);
ResultsField.text = alt.transcript;
/// Only send the recognized speech utterance to Conversation
/// Assistant once we know the user has stopped talking.
if (res.final)
{
string _conversationString = alt.transcript;
Active = false; /// Stop the microphone from listening.
/// Message.
Dictionary<string, object> input = new Dictionary<string, object>();
input["text"] = _conversationString;
MessageRequest messageRequest = new MessageRequest()
{
Input = input,
};
_exampleAssistantV1_script.SendMessageAssistant(messageRequest);
/// Start listening & recording
/// with the microphone.
Active = true;
StartRecording();
}
}
}
}
}
实际上,它最终陷入了else
属性的Active
子句中,这意味着除未设置value
以外,其他一切都很好。
有人可以帮助我理解为什么会发生这种情况,或者为什么没有设置属性,或者为什么服务无法恢复?当然,由于未设置属性,该服务不会恢复,但是我在做什么错了?
public
{
get
{
return _service.IsListening;
}
set
{
if (value && !_service.IsListening)
{
_service.RecognizeModel = ( string.IsNullOrEmpty(_recognizeModel) ? "en-US_BroadbandModel" : _recognizeModel);
_service.DetectSilence = true;
_service.EnableWordConfidence = true;
_service.EnableTimestamps = true;
_service.SilenceThreshold = 0.01f;
_service.MaxAlternatives = 0;
_service.EnableInterimResults = true;
_service.OnError = OnError;
_service.InactivityTimeout = -1;
_service.ProfanityFilter = false;
_service.SmartFormatting = true;
_service.SpeakerLabels = false;
_service.WordAlternativesThreshold = null;
_service.StartListening(OnRecognize, OnRecognizeSpeaker);
}
else if (!value && _service.IsListening)
{
_service.StopListening();
}
}
}