问题
How to fix this?

its failing in past it was working fine but not anymore. (Normally it show green bar in the canvas if you speak in the mic.)
<script type="text/javascript">
var navigator = window.navigator;
navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia({ video: false, audio: true}, function(stream) {
console.log('doing....');
audioContext = new webkitAudioContext();
analyser = audioContext.createAnalyser();
microphone = audioContext.createMediaStreamSource(stream);
javascriptNode = audioContext.createJavaScriptNode(2048, 1, 1);
analyser.smoothingTimeConstant = 0.3;
analyser.fftSize = 1024;
microphone.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioContext.destination);
canvasContext = document.getElementById("test");
canvasContext= canvasContext.getContext("2d");
javascriptNode.onaudioprocess = function() {
console.log('doing.... bla bla');
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var values = 0;
var length = array.length;
for (var i = 0; i < length; i++) {
values += array[i];
}
var average = values / length;
canvasContext.clearRect(0, 0, 300, 130);
canvasContext.fillStyle = '#00ff00';
canvasContext.fillRect(0,130-average,300,130);
}
console.log('doing.... done');
}, function(err) {
console.log("An error occured! " + err);
});
</script>
回答1:
webkitAudioContext()
does not have createJavaScriptNode
and I believe you should not use it anywhere.
Try javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
回答2:
createJavaScriptNode()
has been marked as obsolete (https://developer.mozilla.org/en-US/docs/Web/API/AudioContext.createJavaScriptNode), and it's use is now discouraged. Looks like the method name has been changed to createScriptProcessor()
, here's some doc on it: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext.createScriptProcessor
Hope this helps!
来源:https://stackoverflow.com/questions/25753557/javascript-failing-to-use-createjavascriptnode