首页 > 解决方案 > 使用 BrowserWebSpeech 在网络聊天中更改语音

问题描述

我正在尝试在 Microsoft Webchat 中启用 Text to Speech 选项并使用 createBrowserWebSpeechPonyfillFactory 选项。

如何更改此选项的声音?我想为“en-US”使用女性声音,但它总是使用男性声音播放。

尝试了下面的代码,但没有帮助

    async function speechServicesPonyfillFactory() {
        const speechServicesPonyfillFactory = await window.WebChat.createBrowserWebSpeechPonyfillFactory();

        return options => {

            const ponyfill = speechServicesPonyfillFactory(options);

            var speechSynthesisUtterance = ponyfill.SpeechSynthesisUtterance;
            var speechSynthesis = ponyfill.speechSynthesis;
            var voices = speechSynthesis.getVoices();

            speechSynthesisUtterance.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];
            return {
                SpeechGrammarList: ponyfill.SpeechGrammarList,
                SpeechRecognition: ponyfill.SpeechRecognition,
                speechSynthesis: speechSynthesis,
                SpeechSynthesisUtterance: speechSynthesisUtterance
            }
    };
};

标签: botframework

解决方案


您可以将selectVoice方法作为道具传递给 Web Chat 以选择要使用的语音。查看下面的代码片段和选择语音网络聊天示例以了解更多详细信息。此外,这里是网络聊天支持的语音列表。

window.WebChat.renderWebChat(
  {
    directLine: window.WebChat.createDirectLine({ token }),
    selectVoice: (voices, activity) =>
      // If the activity is in zh-HK, use a voice with keyword "TracyRUS" (Cantonese).
      // Otherwise, use "JessaNeural" (preferred) or "Jessa".
      activity.locale === 'zh-HK'
        ? voices.find(({ name }) => /TracyRUS/iu.test(name))
        : voices.find(({ name }) => /JessaNeural/iu.test(name)) ||
          voices.find(({ name }) => /Jessa/iu.test(name)),
    webSpeechPonyfillFactory
  },
  document.getElementById('webchat')
);

希望这可以帮助!


推荐阅读