i am recording voice on browser and sending it to my API, which is going to send data to Speech-To-Text Api (Azure).
I am keep getting NoMach from the API.
Here is my browser code:
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);
}
});
}
I know my API works, and when i send test data with voiceToText(languageCode, base64String);
in js then it’s also works. but when i record the data and then send, then it says NoMach.