我需要保存麦克风输入以便稍后在 AudioElement 中使用。我这样做是为了获得麦克风输入:
window.navigator.getUserMedia(audio: true).then((MediaStream stream) {
# what should go here?
});
我应该怎么做才能保存音频?
最佳答案
有许多可怕的愚蠢示例,您可以在当前浏览器窗口中播放当前录音。有没有这个用例。对于视频,我可以想象有人想要构建一个类似 Skype 的应用程序,并有一个预览窗口来查看你在视频上看起来是否愚蠢,但音频......
不过我发现了一篇不错的帖子:From microphone to .WAV with: getUserMedia and Web Audio
我移植了链接文章中的部分代码,展示了如何获取数据。
import 'dart:html';
import 'dart:async';
import 'dart:web_audio';
void main() {
window.navigator.getUserMedia(video: true, audio: true).then((MediaStream stream) {
var context = new AudioContext();
GainNode volume = context.createGain();
MediaStreamAudioSourceNode audioInput = context.createMediaStreamSource(stream);
audioInput.connectNode(volume);
int bufferSize = 2048;
ScriptProcessorNode recorder = context.createJavaScriptNode(bufferSize, 2, 2);
recorder.onAudioProcess.listen((AudioProcessingEvent e) {
print('recording');
var left = e.inputBuffer.getChannelData(0);
var right = e.inputBuffer.getChannelData(1);
print(left);
// process Data
});
volume.connectNode(recorder);
recorder.connectNode(context.destination);
/**
* [How to get a file or blob from an object URL?](http://stackoverflow.com/questions/11876175)
* [Convert blob URL to normal URL](http://stackoverflow.com/questions/14952052/convert-blob-url-to-normal-url)
* Doesn't work as it seems blob urls are not supported in Dart
*/
// String url = Url.createObjectUrlFromStream(stream);
// var xhr = new HttpRequest();
// xhr.responseType = 'blob';
// xhr.onLoad.listen((ProgressEvent e) {
// print(xhr.response);
// var recoveredBlog = xhr.response;
// var reader = new FileReader();
//
// reader.onLoad.listen((e) {
// var blobAsDataUrl = reader.result;
// reader.readAsDataUrl(blobAsDataUrl);
// });
// });
// xhr.open('GET', url);
// xhr.send();
/**
* only for testing purposes
**/
// var audio = document.querySelector('audio') as AudioElement;
// audio.controls = true;
// audio.src = url;
});
}
关于dart - 如何保存麦克风音频输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21348509/