使用Azure语音服务将文本转换为语音时如何控制音频播放?

问题描述 投票:0回答:1

下面是我使用 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);
});
javascript c# asp.net-mvc azure-speech azure-ai
1个回答
0
投票

(1)

无法停止正在播放的音频,我想使用相同的 按钮可在播放音频时停止音频。如何控制正在播放的音频。

https://stackoverflow.com/a/53204874/3728901

引用

sound.pause();
sound.currentTime = 0;

(2)

有没有办法通过将音频保存到 .wav 文件来控制音频?

是的。见上文。

(3)

有没有办法在不将其保存到 .wav 文件?

一个模糊的问题。 “白狐就是白狐。” ,您可以通过分割为“一只白狐狸”或“一只白狐狸是一个”等来控制播放

有关解决方法,请参阅 https://learn.microsoft.com/en-us/azure/ai-services/speech-service/rest-text-to-speech?tabs=streaming

© www.soinside.com 2019 - 2024. All rights reserved.