[英]Javascript: Microphone audio to download link?
I'm trying to record audio from the microphone and then add a download link for it. 我正在尝试从麦克风录制音频,然后为其添加下载链接。
This is my current code: 这是我当前的代码:
<!DOCTYPE html>
<html>
<head>
<title>Weird Problem</title>
<script>
function microphone() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(function(stream) {
var recorder = new MediaRecorder(stream);
var chunks = [];
recorder.ondataavailable = function(e) {
chunks.push(e.data);
}
recorder.onstop = function() {
stream.getTracks()[0].stop();
var blob = new Blob(chunks, { type: "audio/ogg; codecs=opus" });
var url = window.URL.createObjectURL(blob);
document.getElementById("player").src = url;
document.getElementById("upload").href = url;
document.getElementById("upload").download = "test.ogg";
}
recorder.start();
setTimeout(function() {
recorder.stop();
}, 5000);
}).catch(function(error) {
console.log(error);
});
}
}
</script>
</head>
<body>
<input type="button" value="Record Microphone" onclick="microphone();">
<br>
<audio controls id="player"></audio>
<br>
<a id="upload">Download Microphone Recording</a>
</body>
</html>
I'm on Chrome, and for me it plays the microphone audio correctly, but when I try to download it, the file won't play or even open. 我使用的是Chrome,对于我来说,它可以正确播放麦克风音频,但是当我尝试下载它时,该文件将无法播放甚至无法打开。
I get various errors that the file contains data in an unknown format, so I think it's something to do with the audio headers. 我收到各种错误消息,该文件包含未知格式的数据,因此我认为这与音频标题有关。
Any help would be highly appreciated! 任何帮助将不胜感激!
It turns out this issue is a bit more complicated than I expected. 事实证明,这个问题比我预期的要复杂一些。
I'm now adapting code from Recorder.js . 我现在正在改编Recorder.js中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.