I am a Javascript programmer beginner.
I am creating a web app that uses web audio API (music visualizer/oscilloscope).
My assignment is:
let audioContext,
masterGain;
function audioSetup() {
let source = 'intro.mp3'; // <<----------I put an offline resource here just it doesn't work for me
audioContext = new (window.AudioContext || window.webkitAudioContext)();
masterGain = audioContext.createGain();
masterGain.connect(audioContext.destination);
let song = new Audio(source),
songSource = audioContext.createMediaElementSource(song);
songSource.connect(masterGain);
song.play();
}
const analyser = audioContext.createAnalyser();
masterGain.connect(analyser);
const waveform = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatTimeDomainData(waveform);
function updateWaveform() {
requestAnimationFrame(updateWaveform);
analyser.getFloatTimeDomainData(waveform);
}
The error is probably because you call audioContext.createAnalyser()
before you've run audioSetup
so audioContext
is undefined
.
It's also not clear what you mean by "offline". If it's a file somewhere, use a file URL.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.