我有一個簡單的音頻分析儀爲我的網站:瀏覽器 - createMediaElementSource不工作
var analyser = document.getElementById('analyzer');
var beat = document.getElementById('track_meta')
var canvas, ctx, source, context, analyser, fbc_array, bars, bar_x,
bar_width, bar_height;
function playAnalyzer() {
context = new AudioContext();
analyser = context.createAnalyser();
canvas = document.getElementById('analyzer');
ctx = canvas.getContext('2d');
source = context.createMediaElementSource(playerE);
source.connect(analyser);
analyser.connect(context.destination);
frameLooper();
}
function frameLooper() {
canvas.width = canwidth;
canvas.height = canheight;
ctx.imageSmoothingEnabled = false;
window.requestAnimationFrame(frameLooper);
fbc_array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(fbc_array);
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
ctx.fillStyle = "white"; // Color of the bars
function valBetween(v, min, max) {
return (Math.min(max, Math.max(min, v)));
}
var beatc = fbc_array[2]/4;
var beatround = Math.round(beatc);
//if (beatround < 10) {
// ctx.globalAlpha = '0.1125';
//}
//else {
// ctx.globalAlpha = '0.' + beatround;
//}
bars = canbars;
for (var i = 0; i < bars; i += canmultiplier) {
bar_x = i * canspace;
bar_width = 2;
bar_height = -3 - (fbc_array[i]/2);
ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
}
}
我的問題是,這並不工作,除非我在Chrome隱身模式,否則我得到這個錯誤:
Uncaught InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode.
我已經在Edge,Firefox和Opera上測試過了,它在那裏工作的很好。我甚至重新安裝了chrome,看看是否能解決問題無濟於事。我只是不明白爲什麼它在隱身模式下工作正常,但沒有其他方式。
所以我的問題是,是否有可能不使用<audio>
元件,而是將分析儀連接到var audio = new Audio
?如果是這樣,我將如何做到這一點?
我剛剛遇到此問題,但我沒有按照您的意思,而是使用「使用JavaScript的HTMLMediaElement而不是使用