最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

audio - How to get Volume level using JavaScript? - Stack Overflow

matteradmin8PV0评论

I developed one web-based monitoring system Using js and WebRTC and now I want to develop notification function if the sound goes beyond some level.

I'm taking permission for video and audio and after permission, I want to use the function for sound notification.

navigator.mediaDevices
  .getUserMedia({
    audio: true,
    video: true
  })
  .then(stream => {
    // Display your local video in #localVideo element
    localVideo.srcObject = stream;
    // Add your stream to be sent to the conneting peer
    pc.addStream(stream);
    // call function for sound check
  }, onError);

I developed one web-based monitoring system Using js and WebRTC and now I want to develop notification function if the sound goes beyond some level.

I'm taking permission for video and audio and after permission, I want to use the function for sound notification.

navigator.mediaDevices
  .getUserMedia({
    audio: true,
    video: true
  })
  .then(stream => {
    // Display your local video in #localVideo element
    localVideo.srcObject = stream;
    // Add your stream to be sent to the conneting peer
    pc.addStream(stream);
    // call function for sound check
  }, onError);
Share Improve this question edited Jul 4, 2020 at 6:22 Raghul SK 1,3905 gold badges24 silver badges35 bronze badges asked Jul 4, 2020 at 5:59 BRIJESH VADODARIYABRIJESH VADODARIYA 531 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use this code.

<!DOCTYPE html>
<html>
<head>
<script>
  navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) {
  audioContext = new AudioContext();
  analyser = audioContext.createAnalyser();
  microphone = audioContext.createMediaStreamSource(stream);
  javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
  analyser.smoothingTimeConstant = 0.8;
  analyser.fftSize = 1024;
  microphone.connect(analyser);
  analyser.connect(javascriptNode);
  javascriptNode.connect(audioContext.destination);
  javascriptNode.onaudioprocess = function() {
      var array = new Uint8Array(analyser.frequencyBinCount);
      analyser.getByteFrequencyData(array);
      var values = 0;

      var length = array.length;
      for (var i = 0; i < length; i++) {
        values += (array[i]);
      }

      var average = values / length;

      if(Math.round(average)>15)
      {
        console.log(Math.round(average));
        document.getElementById("lvl").innerHTML = Math.round(average)-10;
      }
    
  }
  })
  .catch(function(err) {
    /* handle the error */
});
</script>
</head>
<center><p id="lvl" style="font-size:200px"></p><center>
</html>

Copy and make it an HTML file and open it in chrome and make some sound. You will e to know how it's work and if it is the same as you need copy function from the 5th line and make it one separate function and call this function is your code.

Hope it's work for you

Here is an updated snippet that doesn't use onaudioprocess:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<center><p id="lvl" style="font-size:200px"></p><center>

<script>
navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) {
    const audioContext = new AudioContext();
    const analyser = audioContext.createAnalyser();
    const microphone = audioContext.createMediaStreamSource(stream);
    
    analyser.smoothingTimeConstant = 0.8;
    analyser.fftSize = 1024;
    microphone.connect(analyser);
    
    setInterval(() => {
        const array = new Uint8Array(analyser.frequencyBinCount);
        analyser.getByteFrequencyData(array);
        let sum = 0;

        for (let i = 0; i < array.length; i++) {
            sum += array[i];
        }

        const average = sum / array.length;

        if (Math.round(average) > 15) {
            document.getElementById("lvl").textContent = Math.round(average) - 10;
        }
    }, 100);
}).catch(function(err) {
    console.error('Error accessing microphone: ', err);
});
</script>

</body>
</html>

Post a comment

comment list (0)

  1. No comments so far