javascript 使用 HTML5/Flash 录制和上传(到服务器)音频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21366222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:44:44  来源:igfitidea点击:

Record and upload (to server) audio with HTML5/Flash

javascriptjqueryhtmlflashaudio

提问by spyfx

I know that getUserMedia() wont be supported in a few browsers so I have to use more or less a flash based audio recorder. Its very important for me to upload the captured audio to a server via POST even if I could get access to the clientside captured audio would be pretty awesome. So do you guys know a libary/plugin/extension to do this?

我知道一些浏览器不支持 getUserMedia(),所以我或多或少必须使用基于 Flash 的录音机。通过 POST 将捕获的音频上传到服务器对我来说非常重要,即使我可以访问客户端捕获的音频也会非常棒。那么你们知道一个库/插件/扩展来做到这一点吗?

I found some scripts as well like:
https://github.com/mattdiamond/Recorderjs
https://github.com/jwagener/recorder.js/

我还发现了一些脚本,例如:
https: //github.com/mattdiamond/Recorderjs
https://github.com/jwagener/recorder.js/

But the upload doesnt work. I dont know how I could continue.

但是上传不起作用。我不知道如何继续。

回答by Remus Negrota

You can save the recorded data as a blob and then use a FileReader to upload the data via POST with AJAX.

您可以将记录的数据保存为 blob,然后使用 FileReader 通过 POST 和 AJAX 上传数据。

Something similar along these lines:

类似的东西:

function uploadAudio(mp3Data){
    var reader = new FileReader();
    reader.onload = function(event){
        var fd = new FormData();
        var mp3Name = encodeURIComponent('audio_recording_' + new Date().getTime() + '.mp3');
        console.log("mp3name = " + mp3Name);
        fd.append('fname', mp3Name);
        fd.append('data', event.target.result);
        $.ajax({
            type: 'POST',
            url: 'upload.php',
            data: fd,
            processData: false,
            contentType: false
        }).done(function(data) {
            //console.log(data);
            log.innerHTML += "\n" + data;
        });
    };      
    reader.readAsDataURL(mp3Data);
}

This code is taken directly from the gitHub project Recordmp3js available here: https://github.com/nusofthq/Recordmp3js

此代码直接取自 gitHub 项目 Recordmp3js,可在此处获得:https: //github.com/nusofthq/Recordmp3js

It records audio and saves it MP3 format using just HTML5 and JS and then uploads the data on the webserver.

它仅使用 HTML5 和 JS 记录音频并将其保存为 MP3 格式,然后将数据上传到网络服务器上。

It only works on Chrome and Firefox though.

不过它只适用于 Chrome 和 Firefox。