Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当程序运行时,如何让Microsoft Azure Speech to Text开始转录?(统一,C#)_Azure_Unity3d_Speech Recognition_Microsoft Cognitive_Azure Language Understanding - Fatal编程技术网

当程序运行时,如何让Microsoft Azure Speech to Text开始转录?(统一,C#)

当程序运行时,如何让Microsoft Azure Speech to Text开始转录?(统一,C#),azure,unity3d,speech-recognition,microsoft-cognitive,azure-language-understanding,Azure,Unity3d,Speech Recognition,Microsoft Cognitive,Azure Language Understanding,我正在尝试在Unity3D中使用Microsoft Azure的认知服务语音到文本SDK构建一个简单的应用程序。我一直在跟踪,效果很好。本教程唯一的问题是,语音转换为文本是由一个按钮激活的。当你按下按钮时,它将在句子的整个过程中进行转录,你必须再次按下按钮,它才能再次转录。我的问题是,我希望程序一在Unity中运行就开始转录,而不是每次我想转录一个句子时都要按一个按钮 这是代码 public async void ButtonClick() { // Create

我正在尝试在Unity3D中使用Microsoft Azure的认知服务语音到文本SDK构建一个简单的应用程序。我一直在跟踪,效果很好。本教程唯一的问题是,语音转换为文本是由一个按钮激活的。当你按下按钮时,它将在句子的整个过程中进行转录,你必须再次按下按钮,它才能再次转录。我的问题是,我希望程序一在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对象,但无法运行speech to text


任何提示或建议都会令人惊讶。谢谢。

根据您参考的教程脚本中的注释:

// 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.
但这并不像将“
RecognizeOnceAsync
”替换为“
StartContinuousRecognitionAsync
”那么简单,因为行为是不同的
RecognizeOnceAsync
基本上会打开麦克风最多15秒,然后停止收听

相反,使用
StartContinuousRecognitionAsync
StopContinuousRecognitionAsync
将按钮设置为“我是否应该继续收听?”然后更改
Start
函数,只需启动一个新的识别器,并让它等待语音识别器事件发生。下面是我用来启用此功能的脚本:

using UnityEngine;
using UnityEngine.UI;
using Microsoft.CognitiveServices.Speech;

public class HelloWorld : MonoBehaviour
{
    public Text outputText;
    public Button startRecordButton;

    // PULLED OUT OF BUTTON CLICK
    SpeechRecognizer recognizer;
    SpeechConfig config;

    private object threadLocker = new object();
    private bool speechStarted = false; //checking to see if you've started listening for speech
    private string message;

    private bool micPermissionGranted = false;

    private void RecognizingHandler(object sender, SpeechRecognitionEventArgs e)
    {
        lock (threadLocker)
        {
            message = e.Result.Text;
        }
    }
    public async void ButtonClick()
    {
        if (speechStarted)
        {
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false); // this stops the listening when you click the button, if it's already on
            lock(threadLocker)
            {
                speechStarted = false;
            }
        }
        else
        {
            await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false); // this will start the listening when you click the button, if it's already off
            lock (threadLocker)
            {
                speechStarted = true;
            }
        }

    }

    void Start()
    {
        startRecordButton.onClick.AddListener(ButtonClick);
        config = SpeechConfig.FromSubscription("KEY", "REGION");
        recognizer = new SpeechRecognizer(config);
        recognizer.Recognizing += RecognizingHandler;
    }

    void Update()
    {

        lock (threadLocker)
        {
            if (outputText != null)
            {
                outputText.text = message;
            }
        }
    }
}

下面是我使用此功能的gif照片。你不会知道我根本没有点击按钮(而且在录制gif之前只点击了一次)(还有,很抱歉,奇怪的句子,我的同事一直打断我问我在和谁说话)


根据您参考的教程脚本中的注释:

// 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.
但这并不像将“
RecognizeOnceAsync
”替换为“
StartContinuousRecognitionAsync
”那么简单,因为行为是不同的
RecognizeOnceAsync
基本上会打开麦克风最多15秒,然后停止收听

相反,使用
StartContinuousRecognitionAsync
StopContinuousRecognitionAsync
将按钮设置为“我是否应该继续收听?”然后更改
Start
函数,只需启动一个新的识别器,并让它等待语音识别器事件发生。下面是我用来启用此功能的脚本:

using UnityEngine;
using UnityEngine.UI;
using Microsoft.CognitiveServices.Speech;

public class HelloWorld : MonoBehaviour
{
    public Text outputText;
    public Button startRecordButton;

    // PULLED OUT OF BUTTON CLICK
    SpeechRecognizer recognizer;
    SpeechConfig config;

    private object threadLocker = new object();
    private bool speechStarted = false; //checking to see if you've started listening for speech
    private string message;

    private bool micPermissionGranted = false;

    private void RecognizingHandler(object sender, SpeechRecognitionEventArgs e)
    {
        lock (threadLocker)
        {
            message = e.Result.Text;
        }
    }
    public async void ButtonClick()
    {
        if (speechStarted)
        {
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false); // this stops the listening when you click the button, if it's already on
            lock(threadLocker)
            {
                speechStarted = false;
            }
        }
        else
        {
            await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false); // this will start the listening when you click the button, if it's already off
            lock (threadLocker)
            {
                speechStarted = true;
            }
        }

    }

    void Start()
    {
        startRecordButton.onClick.AddListener(ButtonClick);
        config = SpeechConfig.FromSubscription("KEY", "REGION");
        recognizer = new SpeechRecognizer(config);
        recognizer.Recognizing += RecognizingHandler;
    }

    void Update()
    {

        lock (threadLocker)
        {
            if (outputText != null)
            {
                outputText.text = message;
            }
        }
    }
}

下面是我使用此功能的gif照片。你不会知道我根本没有点击按钮(而且在录制gif之前只点击了一次)(还有,很抱歉,奇怪的句子,我的同事一直打断我问我在和谁说话)


您是说希望在启动时转录一句话,然后在单击按钮时转录后续的话吗?或者你是说你想删除按钮并永久转录,而不是一次转录一句话?你是说你想在启动时转录一句话,然后在单击按钮时转录后续的话?或者你是说你想删除按钮,永远地转录,而不是一次一句话?哇!我没想到有人会回答我的问题,但非常感谢你!!哇!我没想到有人会回答我的问题,但非常感谢你!!