下面是我使用 Azure 语音服务将文本转换为音频以供单击按钮的代码,但我无法停止正在播放的音频, 我想在播放音频时使用相同的按钮来停止音频。 如何控制正在播放的音频。 有没有办法控制音频,无论是否保存到 .wav 文件?
const speechConfig = SpeechSDK.SpeechConfig.fromSubscription("key",
"region");
let synthesizer = null;
function initializeSynthesizer() {
const audioConfig = SpeechSDK.AudioConfig.fromDefaultSpeakerOutput();
synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.synthesizing = (s, e) => {
console.log(`Synthesizing: ${e.result.audioData.byteLength
} bytes`);
};
}
function convertTextToSpeech(text) {
if (!synthesizer) {
initializeSynthesizer();
}
let selectedLanguage = TranslationModule.getSelectedLanguage(); // Assuming TranslationModule handles language selection
TranslationModule.translateText(text, selectedLanguage).then(translatedText => {
synthesizer.speakTextAsync(
translatedText,
result => {
if (result.reason === SpeechSDK.ResultReason.SynthesizingAudioCompleted) {
console.log("Speech synthesis completed.");
} else {
console.error("Speech synthesis canceled, " + result.errorDetails);
}
},
error => {
console.error("Error during speech synthesis:", error);
}
);
console.log("Speech synthesis started:", text); // Log synthesis started
}).catch(error => {
console.error('Error translating text for speech:', error);
});
}
$(document).off('click').on('click', '#btnread', function () {
let textToRead = this.getAttribute('data-text'); // Assuming the button has a 'data-text' attribute with the text to read
convertTextToSpeech(textToRead);
});
(1)
无法停止正在播放的音频,我想使用相同的 按钮可在播放音频时停止音频。如何控制正在播放的音频。
见 https://stackoverflow.com/a/53204874/3728901
引用
sound.pause();
sound.currentTime = 0;
(2)
有没有办法通过将音频保存到 .wav 文件来控制音频?
是的。见上文。
(3)
有没有办法在不将其保存到 .wav 文件?
一个模糊的问题。 “白狐就是白狐。” ,您可以通过分割为“一只白狐狸”或“一只白狐狸是一个”等来控制播放