그래서 마이크 입력이있는 크롬에서 작동하는 사운드 비주얼 라이저를 만드는 다음 javascript 코드가 있습니다. 실제 시각화 기능이 잘 작동하지만 실제로 uint8array에서 오디오 데이터를 가져 와서 id = "arrayIndex"라는 단락에 표시 할 수 있는지 궁금합니다. 그러나 array [i]를 표시하려고하면 항상 0으로 나오고 visualizer에서 변경되지 않습니다. 아무도 내가 어떻게 배열의 값을 캡처 할 수있는 아이디어가 있습니까? 감사!HTML 페이지에 오디오 데이터 표시
addEventListener('load', init);
function init() {
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.AudioContext = window.ctx = document.getElementById('c').getContext('2d');
gradient = ctx.createLinearGradient(0, 0, 0, 200);
gradient.addColorStop(1, '#ADD8E6');
gradient.addColorStop(0.65, '#576D74');
gradient.addColorStop(0.45, '#FFAA00');
gradient.addColorStop(0, '#FF0000');
ctx.fillStyle = gradient;
window.AudioContext = window.webkitAudioContext || window.AudioContext;
context = new AudioContext();
analyser = context.createAnalyser();
analyser.fftsize = 512;
analyser.smoothingTimeConstant = 0.9;
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
navigator.getMedia({
audio: true,
video: false
}, function (localMediaStream) {
source = context.createMediaStreamSource(localMediaStream);
console.log(source);
source.connect(analyser);
draw();
}, function (err) {
console.log(err);
});
}
function draw() {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
ctx.clearRect(0, 0, 512, 256);
var barHeight;
for (var i = 0; i < array.length; i++) {
barHeight = 256-array[i];
ctx.fillRect(i * 2, barHeight, 1, 256);
document.getElementById("arrayIndex").innerHTML = barHeight;
}
requestAnimationFrame(draw);
은}
나는 당신의 코드 (또는 그 변종)를 시도해 보았다. 아마도 "arrayIndex"에 오타가있을 수 있습니다. console.log (barHeight)를 추가하여 값을 가져 오는지 확인하십시오. –
안녕하세요, 귀하의 회신에 감사드립니다! barHeight 변수가 페이지에 표시되고 끊임없이 변경된다는 것을 의미합니까? 내 console.log (barHeight)는 나에게 값을 주지만 단락에는 0 만 표시합니다. – Addi