How to stop Microsoft Cognitive TTS audio playing?

Viewed 852

I am using the JavaScript version of Microsoft Cognitive Services Speech SDK from https://github.com/Azure-Samples/cognitive-services-speech-sdk.

The audio is played by the browser when synthesizer.speakTextAsync is called. When the audio is too long I want to stop the audio play but I couldn't find any documentation on how to do that?

Any help is appreciated!

    synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, 
        SpeechSDK.AudioConfig.fromDefaultSpeakerOutput());
    
    synthesizer.speakTextAsync(
      inputText,
      result => {
        if (result) {
          console.log(JSON.stringify(rssesult));
         }
      },
      error => {
        console.log(error);
      }
    );
1 Answers

Stopping audio playing is supported.

You need to create a SpeechSDK.SpeakerAudioDestination() object and use it to create audioConfig like this.

var player = new SpeechSDK.SpeakerAudioDestination();
var audioConfig  = SpeechSDK.AudioConfig.fromSpeakerOutput(player);
var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakTextAsync(
...
);

Then you can call player.pause() and player.resume() to pause and resume the playback.

You can find more info from the doc and sample.

Related