【发布时间】:2019-09-08 20:48:17
【问题描述】:
我正在尝试在 Unity3D 中使用 Microsoft Azure 的认知服务语音转文本 SDK 构建一个简单的应用程序。我关注了this tutorial,效果很好。本教程的唯一问题是 Speech-To-Text 是由一个按钮激活的。当您按下按钮时,它会在一个句子的持续时间内转录,您必须再次按下按钮才能再次转录。我的问题是我希望它在程序在 Unity 中运行后立即开始转录,而不是每次我想转录一个句子时都必须按下按钮。
这是代码。
public async void ButtonClick()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("[My API Key]", "westus");
// Make sure to dispose the recognizer after use!
using (var recognizer = new SpeechRecognizer(config))
{
lock (threadLocker)
{
waitingForReco = true;
}
// Starts speech recognition, and returns after a single utterance is recognized. The end of a
// single utterance is determined by listening for silence at the end or until a maximum of 15
// seconds of audio is processed. The task returns the recognition text as result.
// Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
// shot recognition like command or query.
// For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);
// Checks result.
string newMessage = string.Empty;
if (result.Reason == ResultReason.RecognizedSpeech)
{
newMessage = result.Text;
}
else if (result.Reason == ResultReason.NoMatch)
{
newMessage = "NOMATCH: Speech could not be recognized.";
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = CancellationDetails.FromResult(result);
newMessage = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}";
}
lock (threadLocker)
{
message = newMessage;
waitingForReco = false;
}
}
}
void Start()
{
if (outputText == null)
{
UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it.");
}
else if (startRecoButton == null)
{
message = "startRecoButton property is null! Assign a UI Button to it.";
UnityEngine.Debug.LogError(message);
}
else
{
// Continue with normal initialization, Text and Button objects are present.
}
}
void Update()
{
lock (threadLocker)
{
if (startRecoButton != null)
{
startRecoButton.interactable = !waitingForReco && micPermissionGranted;
}
}
}
我已尝试删除 Button 对象,但语音转文本无法运行。
任何提示或建议都会很棒。谢谢。
【问题讨论】:
-
您是说希望在启动时转录一个话语,然后在单击按钮时转录后续话语?或者您是说您想移除按钮并永久转录,而不是一次只转录一个话语?
标签: azure unity3d speech-recognition microsoft-cognitive azure-language-understanding