Skip to content

Home / Guides / WordPress development

WordPress development

Why Web Audio plays silence from another domain

Audio routed through a Web Audio visualiser goes silent, with no error, when the file comes from another origin. The one-attribute fix and how to spot it.

By Shane Rounce · 2 min read

I use Web Audio on a client's site for a daily audio briefing with a small animated visualiser that moves with the voice. One morning the briefing played, the progress bar moved, and there was no sound. No error in the console, no failed request.

The obvious suspect was the text-to-speech chain that makes the audio. It was fine. The files were complete and played perfectly when opened directly.

The cause: a cross-origin file inside Web Audio

The player does not just play an <audio> element. To drive the visualiser, it routes the element through the Web Audio API with createMediaElementSource(), then into an analyser and out to the speakers.

The day before, the audio files had moved to a separate media domain. The old addresses redirected there. That was the whole problem. When media from another origin is loaded without a CORS request and routed through Web Audio, the browser protects it by outputting silence. The element thinks it is playing. The analyser reads zeros. The speakers get nothing. No error is raised, because nothing has technically failed.

The fix is one attribute

<audio crossorigin="anonymous" src="https://media.example.com/briefing.mp3"></audio>

With crossorigin="anonymous", the browser makes a CORS request for the file. The media domain already sent Access-Control-Allow-Origin: *, so the audio is no longer treated as opaque, and Web Audio can play and analyse it. Both players on the site got the attribute, and the sound came back.

If you create the element in JavaScript, set it before the source:

const el = new Audio();
el.crossOrigin = 'anonymous';
el.src = url;

How to spot Web Audio silence

If audio plays silently through a visualiser or any Web Audio graph, read the analyser while it plays:

const data = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(data);
Math.max(...data); // 0 while audio is playing means the media is cross-origin tainted

All zeros while the timer runs is the signature.

The wider lesson about moving media

Moving media to its own domain or a CDN is usually a good idea. It is also a change of origin, and the browser cares about origins in places you might not expect: Web Audio, drawing images or video onto a canvas, WebGL textures and fonts. Before moving media, search for any code that reads the media's content rather than just displaying it, and make sure those requests use CORS and the media host answers them.

A redirect is not invisible. It can change the origin of everything that follows it.

Need a hand with your next project?

I help with website development, with practical advice and hands-on work. Tell me what you are trying to improve.

Email Shane