【发布时间】:2017-07-12 23:28:17
【问题描述】:
我有一个在 xampp apache 中运行的简单 JS 代码。
var rawFile;
var allText;
var byteArray = [];
rawFile = new XMLHttpRequest();
rawFile.open("GET", "brooklyn.flac", false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
allText = rawFile.responseText;
//alert(allText);
}
}
if (rawFile.status != 200) return byteArray;
for (var i = 0; i < rawFile.responseText.length; ++i) {
byteArray.push(rawFile.responseText.charCodeAt(i) & 0xff)
}
}
rawFile.send(null);
function send(){
var oAjaxReq = new XMLHttpRequest();
var payload = {
config:{
encoding: "FLAC",
sampleRateHertz: 16000,
languageCode:"en-US"
},
audio: {
content: rawFile
}
};
oAjaxReq.open("post", "https://speech.googleapis.com/v1/speech:recognize?key=???", true);
oAjaxReq.setRequestHeader("Content-Type", "application/json");
//object ot json
const jsonPayload = JSON.stringify(payload);
//Length of the jsonPayload
const payLoadLength= jsonPayload.length;
oAjaxReq.setRequestHeader("Content-Length", payLoadLength);
oAjaxReq.withCredentials = true;
//Send Json to Google Cloud Speech Service
oAjaxReq.send(jsonPayload);
}
我正在尝试使用 Google Cloud Speech API。 我正在加载一个名为“brooklyn.flac”的本地音频文件,该文件是我通过 xmlHTTPRequest 从“https://storage.googleapis.com/cloud-samples-tests/speech/brooklyn.flac”下载的。
但是,我总是收到以下错误:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
当我将 payLoad 对象的音频部分从“content: rawFile”更改为“uri:”gs://cloud-samples-tests/speech/brooklyn.flac”时,它可以正常工作。
错误是因为“rawFile”吗? 如果是,如何正确加载此本地文件以将其发送到云服务?
【问题讨论】:
-
使这段代码工作。我没有使用 xmlHTTPRequest 加载本地文件,而是使用了 FileReader。之后,我可以使用 btoa(); 对文件的内容进行编码。并将其发送给谷歌没有任何错误。如果有人需要源代码,请联系我。
标签: javascript xmlhttprequest google-cloud-speech