HTML5中的文本到语音,JavaScript可在所有浏览器上运行

Abh*_*ale 5 html javascript jquery html5 text-to-speech

我创建了一个示例示例,该示例使用仅在Chrome上运行的JavaScript使用HTML5进行文本语音转换,但是当我尝试在其他浏览器(即IE,Mozilla,Safari)上运行该示例时,则不会出现任何问题。我应该怎么做才能在所有浏览器上运行“文本转语音”演示代码。

<code>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
    <script>
        $(document).ready(function () {
            $("#startBtn").on('click', function (e) {
                debugger;
                var u1 = new SpeechSynthesisUtterance('Hello Word..');
                u1.lang = 'en-US';
                u1.pitch = 1;
                u1.rate = 1;
                //u1.voice = voices[10];
                u1.voiceURI = 'native';
                u1.volume = 1;
                speechSynthesis.speak(u1);

            });
        });
    </script>
    <title> </title>
</head>
<body>
    <input type="button" id="startBtn" value="Hello Word.." />
</body>
</html>

</code>
Run Code Online (Sandbox Code Playgroud)

vij*_*oli -1

查看演示

// Check if the browser supports the SpeechSynthesis API
if ('speechSynthesis' in window) {

// Get the text to be spoken from the input field

var textToSpeak = document.getElementById('startBtn').value;

    // Create a new SpeechSynthesisUtterance instance

    var utterance = new SpeechSynthesisUtterance(textToSpeak);
    
    // Use the default voice
    var voices = window.speechSynthesis.getVoices();
    
    utterance.voice = voices[0];

    // Speak the text
    window.speechSynthesis.speak(utterance);
} else {
    alert('Text-to-speech is not supported in this browser.');
}
Run Code Online (Sandbox Code Playgroud)