我正在尝试创建一个 Flask 端点,该端点每 n 秒接受一个音频 blob 并将其写入波形文件:
烧瓶:
@app.route('/api/v01/post/audio-blob/', methods=['POST'])
def api_post_audio_blob():
blob = request.data
with open('file.wav', 'ab') as f:
f.write(blob)
return Response(status=200)
Javascript:
const startButton = document.getElementById("start");
startButton.addEventListener("click", function() {
/* Get client-side audio stream */
var constraints = { audio: true }
var chunks = []
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
const recorder = new MediaRecorder(stream);
recorder.ondataavailable = event => {
chunks.push(event.data);
var blob = new Blob(chunks, {
type: "audio/wav"
});
var xmlhttp = new XMLHttpRequest();
var url = "http://127.0.0.1:5000/api/v01/post/audio-blob/";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "audio/wav");
xmlhttp.send(blob);
};
recorder.start(10000);
})
.catch(function(err) {
/* handle the error */
});
似乎二进制文件写入了 file.wav,但它不播放任何声音。任何人都知道如何解决这个问题?帮助将不胜感激!