我正在浏览器上录制语音并将其发送到我的 API,该 API 会将数据发送到
Speech-To-Text
API (Azure)。
我不断从 API 获取 NoMach。
这是我的浏览器代码:
let languageCode = "da-DK";
let mediaRecorder;
let audioChunks = [];
let isRecording = false;
function startRecording() {
var constraints = {
audio: {
sampleRate: 16000,
channelCount: 1
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function (e) {
audioChunks.push(e.data);
}
mediaRecorder.onstop = function () {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const reader = new FileReader();
reader.onload = function () {
const audioBytes = new Uint8Array(reader.result);
var base64String = btoa(String.fromCharCode.apply(null, audioBytes));
voiceToText(languageCode, base64String);
};
reader.readAsArrayBuffer(audioBlob);
}
mediaRecorder.start();
recordButton.disabled = true;
stopButton.disabled = false;
playButton.disabled = true;
})
.catch(function (err) {
console.error('Error recording audio: ' + err);
});
}
function voiceToText(myLanguageCode, wavData) {
$.ajax({
type: "POST",
url: "https://localhost:7058/api/Conversation/postVoiceToString",
contentType: "application/json", // Set Content-Type header
data: JSON.stringify({ // Stringify the data
LanguageCode: myLanguageCode,
WAV: wavData
}),
success: function (result) {
console.log(result);
},
error: function (req, status, error) {
console.log(status);
}
});
}
我知道我的 API 可以工作,当我在 js 中使用
voiceToText(languageCode, base64String);
发送测试数据时,它也可以工作。但是当我记录数据然后发送它时,它说NoMach。
btoa()
的限制和约束以及在JavaScript中处理字符编码所涉及的复杂性。
btoa()
适用于 ASCII 字符串,而 fromCharCode
适用于 UTF-16,它可能无法正确处理所有字节值。但使用 TextEncoder
API 主要用于编码字符串,而不是二进制数据。我需要作为字节数组字符串发送。当我发送测试数据时(eks。“UklGRqhPBABXQVZ...更长。”)然后它就工作了
如果硬编码字节数组字符串有效,则意味着您的服务器需要特定格式的数据。
let languageCode = "da-DK";
let mediaRecorder;
let audioChunks = [];
let isRecording = false;
function startRecording() {
var constraints = {
audio: {
sampleRate: 16000,
channelCount: 1
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function (e) {
audioChunks.push(e.data);
}
mediaRecorder.onstop = function () {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
const reader = new FileReader();
reader.onload = function () {
const audioBytes = new Uint8Array(reader.result);
const byteArrayString = audioBytesToString(audioBytes);
voiceToText(languageCode, byteArrayString);
};
reader.readAsArrayBuffer(audioBlob);
}
mediaRecorder.start();
// Update UI or disable/enable buttons accordingly
})
.catch(function (err) {
console.error('Error recording audio: ' + err);
});
}
function audioBytesToString(bytes) {
return Array.from(bytes).map(byte => String.fromCharCode(byte)).join(',');
}
function voiceToText(myLanguageCode, byteArrayString) {
$.ajax({
type: "POST",
url: "https://localhost:7058/api/Conversation/postVoiceToString",
contentType: "application/json",
data: JSON.stringify({
LanguageCode: myLanguageCode,
ByteArrayString: byteArrayString
}),
success: function (result) {
console.log(result);
},
error: function (req, status, error) {
console.log(status);
}
});
}
audioBytesToString
函数将包含音频字节的Uint8Array转换为表示字节数组的字符串。使用 String.fromCharCode
将每个字节转换为字符,并将生成的字符连接在一起形成以逗号分隔的字符串。
然后使用 AJAX POST 请求将此字节数组字符串 (
byteArrayString
) 与语言代码一起发送到服务器。
参考: