假设我创建了一个程序,可以播放一个接一个的声音。是否可以在不使用第 3 方库的情况下以某种方式将声音导出为 wav 或 mp3?
我正在尝试构建一个小型音序器,但在此之前,我需要知道这是否可行。
我已经做了我的研究并找到了许多第 3 方库,最著名的似乎是 recorder.js。为了学习,我更喜欢使用纯api。
最佳答案
您可以使用 MediaRecorder API ,不幸的是,is not widely supported yet.. ,但不需要任何外部库。
// creates a simple oscillator, connected to a mediaStream
var ctx = new AudioContext();
var stream = ctx.createMediaStreamDestination();
var osc = ctx.createOscillator();
osc.connect(stream);
osc.type = 'square';
osc.frequency.value = 200;
osc.start();
// pass the stream of our stream destination Node
var rec = new MediaRecorder(stream.stream);
// once it's finished recording
rec.ondataavailable = function(e) {
var audioURL = window.URL.createObjectURL(e.data);
audio.src = audioURL;
audio.play();
};
// start the recorder
rec.start()
btn.onclick = function(){rec.stop();};
<button id="btn"> stop the recording </button>
<audio id="audio" controls></audio>
关于javascript - 将按顺序播放的声音保存为一个音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34502069/