Saving sounds played in sequence as one audio file

不想你离开。 提交于 2020-01-15 06:14:05

问题


Lets say I created a program that plays one sound after another. Is it possible, without the use of 3rd party libraries, to somehow export the sound as wav or mp3?

I am trying to build a little sequencer, but before I do, I need to know if this is possible.

I already did my research and found many 3rd party libraries, the most famous seems to be recorder.js. For the sake of learning, I prefer to use the pure api.


回答1:


Well these can be done with HTML 5 user media api's. Here is a intro from Eric Bidelman.

Since this question isn't about a specific problem in your code, its too broad to answer. So I am including links to some projects on github that I think are simple to understand and use standard API's instead of flash. Should point you in the right direction.

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



回答2:


You could use the MediaRecorder API, which, unfortunately, is not widely supported yet.., but which doesn't need any external library.

// 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>



回答3:


Yes, it's possible. Not natively in the API as such though, you'll have to encode the file yourself based on the data you pull out of Web Audio.

So, basically, you'll have to implement your own version of recorder.js if you don't want to use external dependencies. The best way to figure out how is probably to look at the recorder.js source. It's quite legible!



来源:https://stackoverflow.com/questions/34502069/saving-sounds-played-in-sequence-as-one-audio-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!