How to capture audio in javascript? -
i using getusermedia()
, working on firefox , chrome, yet got deprecated , works on https (in chrome). there other/better way speech input in javascript works on platforms?
e.g. how websites web.whatsapp.com app record audio? getusermedia()
prompts first-time-users permit audio recording, whereas whatsapp application doesn't require user's permission.
the getusermedia()
using looks this:
navigator.getusermedia( { "audio": { "mandatory": { "googechocancellation": "false", "googautogaincontrol": "false", "goognoisesuppression": "false", "googhighpassfilter": "false" }, "optional": [] }, }, gotstream, function(e) { console.log(e); });
chrome require using https, since getusermedia
powerful feature. api access shouldn't work in non-secure domains, api access may bled on non-secure actors. firefox still supports getusermedia
on http, though.
i've been using recorderjs , served purposes well. here code example. (source)
function recordaudio(stream, cfg) { var config = cfg || {}; var bufferlen = config.bufferlen || 4096; var numchannels = config.numchannels || 2; this.context = stream.context; var recordbuffers = []; var recording = false; this.node = (this.context.createscriptprocessor || this.context.createjavascriptnode).call(this.context, bufferlen, numchannels, numchannels); stream.connect(this.node); this.node.connect(this.context.destination); this.node.onaudioprocess = function(e) { if (!recording) return; (var = 0; < numchannels; i++) { if (!recordbuffers[i]) recordbuffers[i] = []; recordbuffers[i].push.apply(recordbuffers[i], e.inputbuffer.getchanneldata(i)); } } this.getdata = function() { var tmp = recordbuffers; recordbuffers = []; return tmp; // returns array of array containing data various channels }; this.start() = function() { recording = true; }; this.stop() = function() { recording = false; }; }
the usage straightforward:
var recorder = new recordaudio(usermedia); recorder.start(); recorder.stop(); var recordeddata = recorder.getdata()
edit: may want check answer if nothing works.
Comments
Post a Comment