Skip to content
Advertisement

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>
<html>
<head>
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <center>
    <h1>Music Player</h1>
    <hr>
    <div id="div"></div>
    <canvas></canvas>
    <p>Frequency plot</p>
    </center>
    <script>
    url = "https://dl.dropbox.com/s/5jyylqps64nyoez/Legends%20never%20die.mp3?dl=0";
    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();
        request.open("GET",url,true);
        request.responseType = "arraybuffer";
        request.onload = ()=>{
            div.innerHTML = "Music loaded, please wait, music will be played soon...";
context.decodeAudioData(request.response,suffer=>{
    source.buffer = suffer;
    source.connect(context.destination);
    source.connect(analyser);
    analyser.connect(context.destination);
    source.start();
    div.innerHTML = "Music is playing... Enjoy!";
    setInterval(()=>{
c.clearRect(0,0,cvs.width,cvs.height);
let array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
let m = 0;
for(m = 0; m < array.length; m++){
    let x = (parseInt(window.innerWidth -20)*m)/array.length;
    c.beginPath();
    c.moveTo(x,150-((100*array[m])/255));
    c.lineTo((parseInt(window.innerWidth -20)*(m+1))/array.length,150-((100*array[m+1])/255));
    c.lineWidth = 1;
    c.strokeStyle = "black";
    c.stroke();

}
},1);
});
        }
        request.send();
    }
   
        loadMusic(url);
    
    </script>
</body>
</html>

```

Advertisement

Answer

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
Advertisement