Microsoft speech recognition stopContinousRecognition does not work

Tadewos Bellete 0 Reputation points
2023-12-08T20:55:02.1566667+00:00
 am trying to create a wrapper for the Microsoft Speech SDK to use in my vue componenet. The issue I am having is after calling startRecognition which calles startContinousRecognitionAsync, no other click even is being detected and it will not stop when I click stop and call the stopRecognition function. Here is the wrapper I created:

export default class SpeechRec {
    constructor(key, region) {
        this.key = key;
        this.region = region;
        this.speechRecognizer = undefined;
        this.stop = false;
    }

    createSpeechConfig() {
        return SpeechSDK.SpeechConfig.fromSubscription(this.key, this.region);
    }

    createRecognizer(speechConfig) {
        return new SpeechSDK.SpeechRecognizer(speechConfig);
    }

    createRecognizerWithLanguage(speechConfig, language) {
        return new SpeechSDK.SpeechRecognizer(speechConfig, language);
    }

    async startRecognition(nextAction) {
        console.log("STOP: ", this.stop)
        const speechConfig = this.createSpeechConfig();
        this.speechRecognizer = this.createRecognizer(speechConfig);
        this.speechRecognizer.BabbleTimeout = 0.75;
        const stopButton = document.querySelector(".stopButton")
        console.log(this.speechRecognizer)

        this.speechRecognizer.startContinuousRecognitionAsync();
        return new Promise(function(resolve, reject) {
            this.speechRecognizer.recognized = function (s, e) {
                if (e.result.reason === SpeechSDK.ResultReason.RecognizedSpeech) {
                    nextAction(e.result.text)
                    resolve(e.result.text);
                }
            };

            this.speechRecognizer.canceled = function (s, e) {
                if (e.reason === SpeechSDK.CancellationReason.Error) {
                    reject(e.reasonDetails);
                }
            };
        }.bind(this));
    }

    stopRecognition() {
        this.speechRecognizer.stopContinuousRecognition();
        this.speechRecognizer.close();
        
    }
}
Here is the vue component:

<template>
  <div>
    <button @click="startSpeechRec">Start</button>
    <button class="stopButton" @click="stopSpeechRec">Stop</button>
  </div>
</template>
<script>
import SpeechRec from '../Util/Speech';
export default {
  name: 'ReadingPage',
  data() {
    return {          
      speechRec: null,
      stopRecognitionRequested: false,
    };
  },
  props: [],
  created() {
    this.fetchReading();

  },
  mounted() {
    const stopButton = document.querySelector(".stopButton")
    stopButton.addEventListener("click", () =>{
            console.log(this.speechRec)
    })
  },
  methods: {
    stopSpeechRec(){
      this.speechRec.stopRecognition()
    },
    startSpeechRec(){
      this.stopRecognitionRequested = false;
      this.speechRec = new SpeechRec('Key', 'eastus');
      const textResult = this.speechRec.startRecognition();
    },

  },
};
</script>
<style>
.highlighted {
  background-color: yellow;
}
</style>
is it because I am using a promise? is there a better way of writing this? Neither the event listener I am adding on mount or the method stopSpeechRec are being called when I press stop.
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,952 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,054 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,269 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.