gpt4 book ai didi

javascript - mediarecorder api 通过 Web Audio api 播放(不是音频元素)

转载 作者:行者123 更新时间:2023-12-01 11:29:33 24 4
gpt4 key购买 nike

我在从 mediaRecorder api 获取捕获的 blob 以在 Chrome 中播放时遇到问题(它适用于 Firefox)。不确定这是否是 Chrome 中的错误。

它报告的错误:

undefined:1 Uncaught (in promise) DOMException: Unable to decode audio data



 window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);

var context = new AudioContext();

var record = document.querySelector('#record');
var stop = document.querySelector('#stop');

if (navigator.getUserMedia) {
console.log('getUserMedia supported.');

var constraints = {
audio: true
};
var chunks = [];

var onSuccess = function(stream) {
var mediaRecorder = new MediaRecorder(stream);
record.onclick = function() {
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("recorder started");
record.style.background = "red";
stop.disabled = false;
record.disabled = true;
}

stop.onclick = function() {
mediaRecorder.stop();
console.log(mediaRecorder.state);
console.log("recorder stopped");
record.style.background = "";
record.style.color = "";

stop.disabled = true;
record.disabled = false;
}

mediaRecorder.onstop = function(e) {
console.log("onstop() called.", e);

var blob = new Blob(chunks, {
'type': 'audio/wav'
});
chunks = [];

var reader = new FileReader();
reader.addEventListener("loadend", function() {
context.decodeAudioData(reader.result, function(buffer) {
playsound(buffer);
},
function(e) {
console.log("error ", e)
});
});
reader.readAsArrayBuffer(blob);
}

mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
}
}

var onError = function(err) {
console.log('The following error occured: ' + err);
}

navigator.getUserMedia(constraints, onSuccess, onError);

} else {
console.log('getUserMedia not supported on your browser!');
}

function playsound(thisbuffer) {
var source = context.createBufferSource();
source.buffer = thisbuffer;
source.connect(context.destination);
source.start(0);
}
<button id="record">record</button>
<button id="stop">stop</button>

最佳答案

我已经按原样使用了您的代码。在 Chrome 浏览器中一切正常。
此问题已在错误 https://codereview.chromium.org/1579693006/ 时得到修复已关闭并添加到 Chrome 管道中。
这不再是一个问题。

关于javascript - mediarecorder api 通过 Web Audio api 播放(不是音频元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com