我试图创建一个频谱图,我需要调用自身的函数,但是setTimeOut不接受" stream"绘图功能需要的参数。 这是我的代码(我知道它不完整)。
var yAxis=0;
var n=0;
var canvas = document.getElementById("can");
var ctx = canvas.getContext("2d");
var context = new AudioContext();
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia;
var analyzer = context.createAnalyser();
var node;
var input;
function gra(){
navigator.getUserMedia({ audio: true }, spect,error);
analyzer.fftSize = 1024;
}
function spect (stream) {
node = context.createScriptProcessor(2048, 1, 1);
input = context.createMediaStreamSource(stream);
input.connect(analyzer);
analyzer.connect(node);
node.connect(context.destination);
var frequencyData = new Uint8Array(analyzer.frequencyBinCount);
if (n==2047){
yAxis=yAxis+1;
n=0;
setTimeOut("spect()",10,stream);
} else {
analyzer.getByteFrequencyData(frequencyData);
ctx.strokeStyle = frequencyData[n];
ctx.moveTo(yAxis,n);
ctx.lineTo(yAxis,n+1);
ctx.stroke();
ctx.beginPath();
n=n+1;
setTimeOut("spect()",10, stream);
}
}
function error () {
alert("The action cannot be completed!")
}
答案 0 :(得分:0)
错字。它应该是setTimeout
而不是setTimeOut
答案 1 :(得分:0)
将函数引用传递给setTimeout
而不是字符串
setTimeout(spect, 10, stream);
答案 2 :(得分:0)
像这样使用setTimeout:
setTimeout(function(){
spect(stream)
},10)
或以es6风格)
setTimeout(() => spect(stream),10)