我在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 = [];
};
答案 0 :(得分:1)
创建MediaStreamDestinationNode之后,您需要将音频图的发声部分连接到它-它不会自动将所有声音发送到audioContext.destination,只是因为它是另一个目标节点。 (您无法记录“所有与发言者有关的内容”,这可能是跨域冲突。)
在创建“目标”节点之后立即添加以下行:
gainNode.connect(dest);
您也确实需要在某个时候调用mediaRecorder.start()-不知道这是否不在您的代码段中。