目前正在try 将一些录制的音频从浏览器发送到Actix-Web服务器,并try 将其保存到磁盘,从而从通用媒体播放器中生成可播放的曲目.我发现这很难做到,特别是因为我以前从未与媒体合作过.我所做的一切都基于一些谷歌搜索和一些GPT的结合. 问题:文件似乎确实已保存到磁盘上.这是一个非零大小的文件,但似乎无法在SLC媒体播放器中播放. 将录音发送到Actix-Web服务器的脚本:
const blob = new Blob(chunks, { type: "audio/mp3" });
const audioURL = URL.createObjectURL(blob);
const audio = new Audio(audioURL);
// create a button
let button = document.createElement("BUTTON");
// creating text to be
//displayed on button
let text = document.createTextNode("Play recording ", audio_rec_num + 1);
button.appendChild(text);
button.setAttribute("num", audio_rec_num);
audio_rec_num++;
document.body.appendChild(button);
button.addEventListener("click", function () {
let num = Number(button.getAttribute("num"));
// audio_chunks[num].play();
// Create FormData object to send the audio data
const formData = new FormData();
formData.append("audio", blob, "recorded_audio.mp3");
// Send the audio data to the server using fetch or XMLHttpRequest
fetch("http://127.0.0.1:8080/track_sample", {
method: "POST",
body: formData,
})
.then((response) => {
if (response.ok) {
console.log("Audio sent successfully");
} else {
console.error("Failed to send audio");
}
})
.catch((error) => {
console.error("Error sending audio: ", error);
});
});
接收数据的处理程序:
async fn track_sampler(req_body: web::Bytes) -> impl Responder {
println!("track sampler hit");
let audio_data = req_body.to_vec();
println!("Received audio data with length: {}", audio_data.len());
// Save the audio data to a WAV file
let file_path = "received_audio.mp3";
let mut file = match File::create(file_path) {
Ok(file) => file,
Err(_) => return HttpResponse::InternalServerError().finish(),
};
// Write the bytes of the audio file to the file on disk
if let Err(_) = file.write_all(&audio_data) {
return HttpResponse::InternalServerError().finish();
}
println!("done");
// Return a success response
HttpResponse::Ok().body("Audio file uploaded successfully")
}
我只需要一些明确的指针就能实现这一点.我不一定需要代码,但这也可能非常有帮助.我做错了什么?我知道我保存得不正确,但如何"正确"呢?
注意:我不希望直接从JS将音频保存到磁盘