将按顺序播放的声音保存为一个音频文件

Saving sounds played in sequence as one audio file

假设我创建了一个程序,可以一个接一个地播放声音。是否可以在不使用第 3 方库的情况下以某种方式将声音导出为 wav 或 mp3?

我正在尝试构建一个小型音序器,但在此之前,我需要知道这是否可行。

我已经做了研究,发现了很多第 3 方库,最著名的似乎是 recorder.js。为了学习,我更喜欢使用纯api.

是的,这是可能的。虽然在 API 中不是原生的,但您必须根据从网络音频中提取的数据自行对文件进行编码。

所以,基本上,如果您不想使用外部依赖项,则必须实现自己的 recorder.js 版本。找出方法的最好方法可能是查看 recorder.js 来源。非常清晰!

嗯,这些可以用 HTML 5 个用户媒体 api 来完成。 Here is a intro from Eric Bidelman.

由于此问题与您的代码中的特定问题无关,因此范围太广,无法回答。因此,我在 github 上包含了一些我认为易于理解并使用标准 API 而不是 flash 的项目的链接。应该为您指明正确的方向。

  1. julianburr/js-audiorecorder
  2. minervaproject/user-media-recorder

您可以使用 MediaRecorder API, which, unfortunately, 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>