如何将Javascript音频Blob编写为Python Wave? [英] How to write Javascript audio blob to Python wave?

查看:69
本文介绍了如何将Javascript音频Blob编写为Python Wave?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Flask端点,该端点每n秒接收一次音频blob,并将其写入wave文件:

I'm trying to create a Flask endpoint that accepts an audio blob every n seconds and write it to a wave file:

烧瓶:

@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,但没有播放任何声音.有人知道如何解决这个问题吗?帮助将不胜感激!

It seems like binary is written to file.wav, but it doesn't play any sound. Anyone knows how to resolve this? Help would be much appreciated!

推荐答案

上面的代码有效.尝试了另一个解决问题"的音频播放器

the code above works. Tried another audio player which resolved the 'issue'

这篇关于如何将Javascript音频Blob编写为Python Wave?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆