How to end Google Speech-to-Text streamingRecognize gracefully and get back the pending text results?

后端 未结 3 1230
心在旅途
心在旅途 2020-12-20 20:58

I\'d like to be able to end a Google speech-to-text stream (created with streamingRecognize), and get back the pending SR (speech recognition) results.

In a nutshell,

3条回答
  •  余生分开走
    2020-12-20 21:30

    This: "I'm looking for a potential workaround." - have you considered extending from SpeechClient as a base class? I don't have credential to test, but you can extend from SpeechClient with your own class and then call the internal close() method as needed. The close() method shuts down the SpeechClient and resolves the outstanding Promise.

    Alternatively you could also Proxy the SpeechClient() and intercept/respond as needed. But since your intent is to shut it down, the below option might be your workaround.

    const speech = require('@google-cloud/speech');
    
    class ClientProxy extends speech.SpeechClient {
      constructor() {
        super();
      }
      myCustomFunction() {
        this.close();
      }
    }
    
    const clientProxy = new ClientProxy();
    try {
      clientProxy.myCustomFunction();
    } catch (err) {
      console.log("myCustomFunction generated error: ", err);
    }
    

提交回复
热议问题