使用Javascript录制音频并与Microsoft Cognitive Services一起使用



正如标题所示,我正在用ASP制作一个web应用程序。Net Core MVC,记录来自用户计算机的音频:

function startRecording(btn) {
var countdown = 5;
var audioChunks = [];
var audioContext = new AudioContext();
var mediaRecorder = null;
var options = { mimeType: 'audio/webm' }; //I tried changing it to audio/wav but it doesn't work
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function (stream) {
mediaRecorder = new MediaRecorder(stream, options);
mediaRecorder.start();
setTimeout(function () {
mediaRecorder.stop();
stream.getTracks().forEach(track => track.stop());
btn.text("Waiting for the results");
}, countdown * 1000);
mediaRecorder.addEventListener("dataavailable", function (event) {
audioChunks.push(event.data);
});
mediaRecorder.addEventListener("stop", function () {
var blob = new Blob(audioChunks, { type: 'audio/wav' });
var formData = new FormData();
formData.append('audioBlob', blob);
$.ajax({
url: '@Url.Action("ProcessAudio", "Home")',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (data) {
// Display the result in the modal
$('#modalContent').html(data);
// Show the modal
$('#myModal').modal('show');
},
error: function () {
console.log('Error processing audio');
}
});
});
})
.catch(function (error) {
console.log('Error starting recording:', error);
});
}

然后我处理并保存音频到服务器内的一个文件:

[HttpPost]
public IActionResult ProcessAudio(IFormFile audioBlob)
{
string filePath = Path.Combine(_env.WebRootPath, "Resources", "recording.wav");
if (audioBlob != null && audioBlob.Length > 0)
{
using (var stream = audioBlob.OpenReadStream())
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
stream.CopyTo(fileStream);
}
}
}
var model = new AudioReadingModel(filePath);
model.RecordAndCalculate();
return PartialView("ShowResults", model);
}

但是,当我在代码中点击以下行时:

var recognizer = new Microsoft.CognitiveServices.Speech.SpeechRecognizer(config, AudioConfig.FromWavFileInput(AudioPath));

它给了我以下的异常:

系统。AggregateException: '发生一个或多个错误。'(异常错误代码:0xa (SPXERR_INVALID_HEADER))'

文件有。wav扩展名,我可以在我的电脑上听它,所以我真的需要帮助找出发生了什么。有人能帮帮我吗?

您可以尝试下面的代码来识别音频文件。

var speechConfig = SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey.value, serviceRegion.value);    
speechConfig.speechRecognitionLanguage = "en-US";
var audioConfig  = SpeechSDK.AudioConfig.fromWavFileInput(audioFile);
recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);

最新更新