我有一个在 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”吗?如果是,如何正确加载此本地文件以将其发送到云服务?