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.