如何将Javascript客户端库导入我的网站?

时间:2019-01-15 03:00:54

标签: google-text-to-speech

我正在尝试在我的网站中使用Google的文本到语音的api(特别是使用wavenet语音),该api读取放置在文本区域中的代码。每当我运行代码时,我都会不断遇到以下错误,但我不明白为什么: “未捕获的ReferenceError:需求未定义     在saveTextAsFile(index.html:132)”

我只是想使事情正常进行,然后再对Google网站上的代码进行过多更改,并且我知道代码的require部分通常无法在网站上使用,但是我已经没有其他选择地寻找替代品。有人可以帮我弄清楚我需要如何更改代码吗?

<!DOCTYPE html>
<html>
<table style="width:100%">

<tr>
	<th colspan="3">
	</th>
</tr>
<tr>
	<th colspan="3">
		<textarea placeholder="Enter your text here." style="width:100%" wrap="soft" id="inputTextToSave" cols="80" rows="25"></textarea> 
	</th>

</tr>

<tr>
	<th colspan="3">
		<button style="width:100%" onclick="saveTextAsFile()">
			<strong>
				LISTEN!
			</strong>
		</button>
	</th>
</tr>


<script>

	function saveTextAsFile()
    {
	  // Creates a client
	  var textToSave = document.getElementById("inputTextToSave").value;
	  var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
	  var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
	  var fileNameToSaveAs = "Input_File";

      // Imports the Google Cloud client library
      const textToSpeech = require('@google-cloud/text-to-speech');
      // Creates a client
      const client = new textToSpeech.TextToSpeechClient();

        // Construct the request
        const request = {
          input: {text: textToSave},
          // Select the language and SSML Voice Gender (optional)
          voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
          // Select the type of audio encoding
          audioConfig: {audioEncoding: 'MP3'},
      };

      // Performs the Text-to-Speech request - HERE ERROR
      client.synthesizeSpeech(request, (err, response) => {
      	if (err) {
      		console.error('ERROR:', err);
      		return;
      	}

        // Write the binary audio content to a local file
        localStorage.setItem('output.mp3', response.audioContent);
      });
  	}

    function destroyClickedElement(event)
    {
    	document.body.removeChild(event.target);
    }

    function loadFileAsText()
    {
    	var fileToLoad = document.getElementById("fileToLoad").files[0];

    	var fileReader = new FileReader();
      	fileReader.onload = function(fileLoadedEvent) 
      	{
      		var textFromFileLoaded = fileLoadedEvent.target.result;
      		document.getElementById("inputTextToSave").value = textFromFileLoaded;
      	};
      	fileReader.readAsText(fileToLoad, "UTF-8");
    }

  </script>

</body>
</html>

</html>

0 个答案:

没有答案