工作中遇到的,具体服务看老板买了什么。
const xhr = new XMLHttpRequest();
// 发送GET请求
xhr.open('GET', '后端返回音频路由', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (xhr.status === 200) {
const buffer = xhr.response;
axios({
url: 服务API + 服务token,
method: 'post',
headers: {
'Content-type': 'application/octet-stream',
},
data: buffer,
}).then((res) => {
if (res && res.data) {
let s = '';
res.data.sentences.forEach((ele) => {
if (ele.text) s += ele.text;
});
console.log('语音转文字结果:', s);
return s;
}
});
}
};
// 发送请求
xhr.send();