【问题标题】:Microsoft Azure Text to Speech - Stop Recording On Button Press - StopRecognitionOnceAsync() Not Working?Microsoft Azure 文本转语音 - 按下按钮时停止录制 - StopRecognitionOnceAsync() 不起作用?
【发布时间】:2020-07-15 17:14:01
【问题描述】:

对于任何术语错误,请提前致歉,我是一名学生,并且尽我所能尽可能地清楚!并提前感谢您的帮助!

我正在尝试使用 Azure Speech-To-Text 服务。我希望用户能够按下开始和停止按钮来记录自己并打印出转录。我的应用最终将成为 React 前端和 Rails 后端,但现在我只是在尝试理解和完成演示。

我对文档感到困惑,但能够让事情顺利进行。然而,现在它只是不断地听演讲者,而且从未停止过。

一旦按下按钮,我想使用stopContinuousRecognitionAsync()recognizer.close(),但我似乎无法让它工作。我得到的最远的结果是仅在按下停止按钮后才记录结果,但它会继续收听并打印结果。我也尝试过使用recognizer.close() -> recognizer = undefined,但无济于事。我猜测由于异步行为,它会在记录结果之前关闭识别器。

我尝试过的最新代码如下。此结果在开始点击时开始收听并在停止时打印语音,但会继续收听并记录结果。

    // subscription key and region for speech services.
    var subscriptionKey, serviceRegion;
    var authorizationToken;
    var SpeechSDK;
    var recognizer;


    document.addEventListener("DOMContentLoaded", function () {
      startRecognizeOnceAsyncButton = document.getElementById("startRecognizeOnceAsyncButton");
      subscriptionKey = document.getElementById("subscriptionKey");
      serviceRegion = document.getElementById("serviceRegion");
      phraseDiv = document.getElementById("phraseDiv");

      startRecognizeOnceAsyncButton.addEventListener("click", function () {
        startRecognizeOnceAsyncButton.disabled = true;
        phraseDiv.innerHTML = "";

        // if we got an authorization token, use the token. Otherwise use the provided subscription key
        var speechConfig;
        if (authorizationToken) {
          speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion.value);
        } else {
          speechConfig = SpeechSDK.SpeechConfig.fromSubscription(“API_KEY”, serviceRegion.value);
        }

        speechConfig.speechRecognitionLanguage = "en-US";
        var audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
        recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);


        recognizer.startContinuousRecognitionAsync(function () {}, function (err) {
        console.trace("err - " + err);});


        
        stopButton = document.querySelector(".stopButton")
        
        stopButton.addEventListener("click", () =>{
            console.log("clicked")
      
                recognizer.recognized = function(s,e) {
                    console.log("recognized text", e.result.text)
                }
            })
    


   
      }); 

【问题讨论】:

    标签: javascript speech-recognition speech-to-text microsoft-cognitive speech


    【解决方案1】:

    假设识别器在代码之外是正确的,有一些事情需要改变以获得你想要的结果。

    1. 在调用 startContinuousRecognition() 之前,应将事件挂钩到识别器。

    2. 在停止按钮处理程序中,调用 stop。我还将在开始按钮单击处理程序之外挂钩停止事件。

    快速键入的更改,未编译。 :-)

    startRecognizeOnceAsyncButton.addEventListener("click", function () {
       startRecognizeOnceAsyncButton.disabled = true;
    
       //div where text is being shown
       phraseDiv.innerHTML = "";
    
       // The event recognized signals that a final recognition result is received.
       recognizer.recognized = function(s,e) {
          console.log("recognized text", e.result.text)
       }
    
       //start listening to speaker
       recognizer.startContinuousRecognitionAsync(function () {}, function (err) {
          console.trace("err - " + err);});
    });
    
    stopButton = document.querySelector(".stopButton")
            
    stopButton.addEventListener("click", () =>{
       console.log("clicked");
       recognizer.stopContinuousRecongition();
    };
    

    【讨论】:

    • 感谢 Ryan - 我更新了帖子中的代码,以更全面地展示识别器是如何被调用的。 ri 尝试了您的解决方案,但它给了我在 start 事件内外使用 stopContinuousRecognitionAsync 的错误。我不确定为什么,有时它告诉我它不是识别器的功能,有时它不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多