Skip to content

Web Audio API is not working properly, in google chrome browser

I’ve created a simple music player, which creates a bufferArray for a particular audio URL to play the music.

It is working fine in many of my cellphone’s browser, so I guess there is no cross origin issue for audio URL.

however chrome is not playing audio.

Also I’ve created a Uint8Array for plotting frequency data inside canvas, while many browsers are plotting frequency graph in canvas successfully, chrome is not doing so!

Take a look at what I’ve tried so far!

<!DOCTYPE html>
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>Music Player</h1>
    <div id="div"></div>
    <p>Frequency plot</p>
    url = "";
    const div = document.querySelector("#div");
    const cvs = document.querySelector("canvas");
    cvs.width = window.innerWidth - 20;
    cvs.height = 200;
    const c = cvs.getContext("2d");
    function loadMusic(url){
 div.innerHTML = "Loading music, please wait...";
        const context = new AudioContext();
        const source = context.createBufferSource();
        const analyser = context.createAnalyser();
        let request = new XMLHttpRequest();"GET",url,true);
        request.responseType = "arraybuffer";
        request.onload = ()=>{
            div.innerHTML = "Music loaded, please wait, music will be played soon...";
    source.buffer = suffer;
    div.innerHTML = "Music is playing... Enjoy!";
let array = new Uint8Array(analyser.frequencyBinCount);
let m = 0;
for(m = 0; m < array.length; m++){
    let x = (parseInt(window.innerWidth -20)*m)/array.length;
    c.lineTo((parseInt(window.innerWidth -20)*(m+1))/array.length,150-((100*array[m+1])/255));
    c.lineWidth = 1;
    c.strokeStyle = "black";





This is more a couple of observations than a complete solution.

The code given worked for me on Edge, Chrome and Firefox on Windows 10.

On IOS 14 Safari and IOS 14 Chrome it seemed to stop after putting out the loading message.

This MDN reference used a ‘cross browser’ method to create audiocontext so I added this line:

 var AudioContext = window.AudioContext || window.webkitAudioContext;

before this line:

 const context = new AudioContext();

[edit: have just confirmed at caniuse that -webkit prefix needed by Safari]

That seemed to do the trick in as much as the rest of the code was executed. However, there was no sound and it appeared the audio was not playing. The plot also showed just a single horizontal line.

Is this a manifestation of IOS’s requirement that there must be some user interaction before audio will actually be played?

I’m pretty sure the audio was loaded as there was a noticeable pause at that point. I suspect that there will have to be a button which when clicked actually starts the playing.

User contributions licensed under: CC BY-SA
8 People found this is helpful