语音服务-文字转换语音范例(text-to-speech)

今天反过来,试试文字转换语音的范例
https://ithelp.ithome.com.tw/upload/images/20201013/20130663xMT20Zred0.png

跟前天的语音服务-语音转换文字范例(from-file)一样
index.htmltoken.php部署到一个Azure Web App中
完成的页面如下,介面就类似前两天的范例
https://ithelp.ithome.com.tw/upload/images/20201013/20130663fOZ7W2iMTw.png
基本上就跟昨天的程序类似
只是要先在Input Text的文字框中输入文字
按下按钮让结果显示在Result的文字框中

程序码的部分
到speechConfig的建立为止就跟昨天一样
接着不需要指定辨识语言以及建立audioConfig
而是使用SDK的SpeechSynthesizer物件

synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);

首先先new一个SpeechSDK.SpeechSynthesizer物件
并将上面建立的speechConfig作为参数传入
接着使用speakTextAsync以非同步的将语音转换为文字
并将文字框的内容(phraseDiv.value)传入

let inputText = phraseDiv.value;
        synthesizer.speakTextAsync(
          inputText,
          function (result) {
            startSpeakTextAsyncButton.disabled = false;
            if (result.reason === SpeechSDK.ResultReason.SynthesizingAudioCompleted) {
              resultDiv.innerHTML += "synthesis finished for [" + inputText + "].\n";
            } else if (result.reason === SpeechSDK.ResultReason.Canceled) {
              resultDiv.innerHTML += "synthesis failed. Error detail: " + result.errorDetails + "\n";
            }
            window.console.log(result);
            synthesizer.close();
            synthesizer = undefined;
          },
          function (err) {
            startSpeakTextAsyncButton.disabled = false;
            resultDiv.innerHTML += "Error: ";
            resultDiv.innerHTML += err;
            resultDiv.innerHTML += "\n";
            window.console.log(err);

            synthesizer.close();
            synthesizer = undefined;
        });

结果(result)便会显示在Result的文字框中


<<:  Day 28 -- Rails 实作 Ransack 简易搜寻及排序功能

>>:  Media queries

Day30 小实作 Image Preview Box

终於完赛了,很感谢iT邦帮忙铁人赛这个活动,让我在30天内因为要发文而不断的学习,原本是一个下了班...

将质化转为量化的数据分析

不管是问卷分析还是在看用户的使用资料,都充斥着许多密密麻麻的资料,这些值化的资料最後都会转换成量化的...

16.MYSQL搜寻特殊字元

在前一篇文章中,我们有提到如果要搜寻部分字元的话,可以使用 _ 以及 % 这两种 但是如果想要将 _...

近似最短路径 (3)

11.3 一些 Leetcode 例题 再来看一些有趣的最短路径变化题吧! Leetcode 882...

[Angular] Day18. Introduction to services and dependency injection

在开发专案时你一定会使用到 Service 的技巧,Service 是一个广泛的类别,包括 app ...