Stopping Audio Playback Mid-Stream with Microsoft Neural TTS Service and Speech SDK

MD SHAKIL KHAN 0 Reputation points
2024-10-27T06:32:10+00:00

I'm working with the Microsoft Neural Text-to-Speech (TTS) service using the Speech SDK. I've successfully implemented audio playback, but I'm facing a challenge with controlling the playback mid-stream.

My question is: How can I implement a feature to stop the audio playback at any given moment while using the Speech SDK with the Neural TTS service?

Specifically, I'm looking for:

The appropriate method or function to call for stopping audio playback

Any considerations or best practices for handling this interruption

Examples or code snippets demonstrating how to implement this feature (in Javascript)

I appreciate any guidance or solutions you can provide. Thank you in advance for your help!

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
2,078 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 22,031 Reputation points Volunteer Moderator
    2024-10-27T09:56:41.93+00:00

    Hello MD SHAKIL KHAN,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to stop audio playback mid-stream using the Microsoft Neural Text-to-Speech (TTS) service with the Speech SDK in JavaScript.

    You can call the close method on the AudioConfig object to stop the audio playback using JavaScript, Where startSynthesis starts the speech synthesis and stopSynthesis stops the audio playback by calling the close method on the audioConfig object. An example of how you can implement ii is here below:

    const sdk = require("microsoft-cognitiveservices-speech-sdk");
    // Initialize the Speech SDK
    const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
    const audioConfig = sdk.AudioConfig.fromDefaultSpeakerOutput();
    // Create a speech synthesizer
    const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
    // Function to start speech synthesis
    function startSynthesis(text) {
        synthesizer.speakTextAsync(
            text,
            result => {
                if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
                    console.log("Synthesis completed.");
                } else {
                    console.error("Synthesis failed. Error details: " + result.errorDetails);
                }
            },
            error => {
                console.error("Error during synthesis: " + error);
            }
        );
    }
    // Function to stop speech synthesis
    function stopSynthesis() {
        audioConfig.close();
        console.log("Audio playback stopped.");
    }
    

    You can modify it to suite your application.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


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.