【发布时间】:2018-08-28 11:14:06
【问题描述】:
我在 html 页面中有一个 .wav 音频,并希望使用 javascript 录制它。我想从扬声器录音。音频正在播放,正在发送到扬声器,并且支持格式,但 mediaRecorder() 没有录制声音。下载文件时,它是空的。
我不确定接下来要检查什么?
//start playing sound button, html page
document.querySelector(".start").addEventListener("click", function() {
audioZero.play();
});
//start recording sound button, html page
document.querySelector(".startrec").addEventListener("click", function() {
mediaRecorder.start();
console.log("recorder started");
});
//stop recording sound button, html page
document.querySelector(".stoprec").addEventListener("click", function() {
mediaRecorder.requestData();
mediaRecorder.stop();
});
let audioContext = new AudioContext();
//get sound
let audioZero = document.getElementById("audio0")
// creates a link between audio context and file
const maracas = audioContext.createMediaElementSource(audioZero)
let gainNode = audioContext.createGain()
maracas.connect(gainNode)
// creates link to the speaker
gainNode.connect(audioContext.destination);
console.log(audioContext.destination);
gainNode.gain.value = 1;
//Gets stream of data from the speaker output - gives the ability to store
const dest = audioContext.createMediaStreamDestination();
//This records the stream
var mediaRecorder = new MediaRecorder(dest.stream);
let chunks = [];
//when data is available an event is raised, this listens for it
mediaRecorder.ondataavailable = function(evt) {
console.log(evt, evt.data);
chunks.push(evt.data);
};
mediaRecorder.onstop = function(evt) {
// Make blob out of our blobs, and open it.
var blob = new Blob(chunks, { 'type' : "audio/webm;codecs=opus" });
var anchorTag = document.createElement("a");
anchorTag.setAttribute('download', 'download');
anchorTag.innerHTML="download me";
// creates the download link
anchorTag.href = URL.createObjectURL(blob);
document.body.appendChild(anchorTag);
chunks = [];
};
【问题讨论】:
标签: html web-audio-api mediarecorder-api