function createAudioElement(blob) {
var aud = document.getElementById("sound");
aud.controls = true;
aud.src = blob;
aud.type = 'audio/webm';
}
var recorder;
var publicBlobObj;
function start() {
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
// store streaming data chunks in array
const chunks = [];
// create media recorder instance to initialize recording
recorder = new MediaRecorder(stream);
// function to be called when data is received
recorder.ondataavailable = e => {
// add stream data to chunks
chunks.push(e.data);
// if recorder is 'inactive' then recording has finished
if (recorder.state == 'inactive') {
// convert stream data chunks to a 'webm' audio format as a blob
publicBlobObj = new Blob(chunks, { type: 'audio/webm' });
// convert blob to URL so it can be assigned to a audio src attribute
createAudioElement(URL.createObjectURL(publicBlobObj));
}
};
// start recording with 1 second time between receiving 'ondataavailable' events
recorder.start();
// setTimeout to stop recording after 4 seconds
}).catch(console.error);
}
function stop() {
recorder.stop();
var xhr = new XMLHttpRequest();
var data = new FormData;
data.append("sound", publicBlobObj);
xhr.open('POST', '/User/Sound', true);
// Listen to the upload progress.
xhr.send(data);
}
function createAudioElement(blob) {
var aud = document.getElementById("sound")
aud.controls = true
aud.src = blob
aud.type = 'audio/webm'
}
var recorder
var publicBlobObj
function start(callback) {
navigator
.mediaDevices
.getUserMedia({ audio: true })
.then(stream => {
const chunks = []
recorder = new MediaRecorder(stream)
recorder.ondataavailable = e => {
chunks.push(e.data)
if (recorder.state == 'inactive') {
publicBlobObj = new Blob(chunks, { type: 'audio/webm' })
createAudioElement(URL.createObjectURL(publicBlobObj))
if (callback) callback()
}
}
recorder.start()
})
.catch(console.error)
}
function stop() {
recorder.stop()
var xhr = new XMLHttpRequest()
var data = new FormData
data.append("sound", publicBlobObj)
xhr.open('POST', '/User/Sound', true)
xhr.send(data)
}
start(() => {
// вот тут уже и recorder и publicBlobObj имеют значение
})
function start() {
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
// store streaming data chunks in array
const chunks = [];
// create media recorder instance to initialize recording
recorder = new MediaRecorder(stream);
// function to be called when data is received
recorder.ondataavailable = e => {
// add stream data to chunks
chunks.push(e.data);
// if recorder is 'inactive' then recording has finished
if (recorder.state == 'inactive') {
// convert stream data chunks to a 'webm' audio format as a blob
window.publicBlobObj = new Blob(chunks, { type: 'audio/webm' });
// convert blob to URL so it can be assigned to a audio src attribute
createAudioElement(URL.createObjectURL(publicBlobObj));
}
};
// start recording with 1 second time between receiving 'ondataavailable' events
recorder.start();
// setTimeout to stop recording after 4 seconds
}).catch(console.error);
}
function stop() {
recorder.stop();
var xhr = new XMLHttpRequest();
var data = new FormData;
data.append("sound", window.publicBlobObj);
xhr.open('POST', '/User/Sound', true);
// Listen to the upload progress.
xhr.send(data);
}