【问题标题】:MediaRecoder not recording HTML5 AudioMediaRecorder 不录制 HTML5 音频
【发布时间】: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


    【解决方案1】:

    创建 MediaStreamDestinationNode 后,您需要将音频图的发声部分连接到它 - 它不会因为它是另一个目标节点而自动将所有声音发送到 audioContext.destination。 (您不能录制“所有发送给扬声器的内容” - 这可能是跨域违规。)

    在创建“dest”节点后立即添加此行:

    gainNode.connect(dest);
    

    您也确实需要在某个时候调用 mediaRecorder.start() - 不确定这是否不在您的 sn-p 中。

    【讨论】:

    • 谢谢,它成功了。 mediaRecorder 位于按钮链接之一中。你能解释一下这与 gainNode.connect(audioContext.destination); 有何不同吗?因为我以为我用它把声音连接到了扬声器。
    • 将 audioContext.destination 视为“扬声器”是非常正确的——但尽管 MediaStreamDestinationNode 是一种目标节点,但它与扬声器不是同一个节点。目的地节点就是任何最终目的地——例如,不能连接到其他任何东西。您需要将音频馈送独立连接到媒体记录器。
    • 简而言之,除了“扬声器”之外,您的图表中还可以包含其他目标节点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多