Skip to content
Advertisement

Why does audioContext mute the sound

I’m trying to do a visualiser like this: Visualiser Audio js

But with the file that is on my pc not one that the customer can choose. Like here the file is on my pc.

<audio src="a.mp3" id="audioHTML" controls></audio>

I found this person doing it but it’s the customer who choose the file they want, so how can I do it ?

I tried this but the sound is muted and that’s my problem. (when I delete ‘var context = new AudioContext();’ the sound is back but obviously there is not the visualiser)

'use strict'

var audio = document.getElementById("audioHTML");
  

function playAudio() {
    audio.play();

    var context = new AudioContext();
    var src = context.createMediaElementSource(audio);
    var analyser = context.createAnalyser();

    var canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    var ctx = canvas.getContext("2d");

    src.connect(analyser);
    analyser.connect(context.destination);

   
    analyser.fftSize = 256;
    
    var bufferLength = analyser.frequencyBinCount;
    console.log(bufferLength);
    
   
    var dataArray = new Uint8Array(bufferLength);

    var WIDTH = canvas.width;
    var HEIGHT = canvas.height;

    var barWidth = (WIDTH / bufferLength) * 2.5;
    var barHeight;
    var x = 0;

    function renderFrame() {
      requestAnimationFrame(renderFrame);

      x = 0;

      analyser.getByteFrequencyData(dataArray);

      ctx.fillStyle = "#000";
      ctx.fillRect(0, 0, WIDTH, HEIGHT);

      for (var i = 0; i < bufferLength; i++) {
        barHeight = dataArray[i];
        
        var r = barHeight + (25 * (i/bufferLength));
        var g = 250 * (i/bufferLength);
        var b = 50;

        ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
        ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);

        x += barWidth + 1;
      }
    }

    audio.play();
    renderFrame();
   }
#thefile {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
}

#canvas {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

audio {
  position: fixed;
  left: 10px;
  bottom: 10px;
  width: calc(100% - 20px);
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" href="style.css" />
</head>
    <body>

        <div id="content">
            <canvas id="canvas"></canvas>
            <button onclick="playAudio()">click</button>
            <audio src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" id="audioHTML" controls></audio>
        </div>

        <script src="audioVisualiser.js"></script>
    </body>
</html>

Advertisement

Answer

For your audio element, try setting the crossorigin attribute to use-credentials.

<audio src="https://..." crossorigin="use-credentials" id="audioHTML" controls></audio>

Without it, the MediaElementSourceNode in your AudioContext may just output silent samples, to prevent scripts from loading cross-domain resources.

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