2

I have user the MediaStream Recording API for recording audio using microphone, the recording works well but in case of don't allow the browser to record a voice then click on the record button it's not showing any error,

What I want is in case of user don't allow voice and still clicks on record button browser again asp for audio recording permission.

I have tried several options with the following code but unfortunately, it does not work, Can anyone help me out with this issue?

The same question or similar question asked on reprompt for permissions with getUserMedia() after initial denial but still, there is no resolution for that issue, hope for the best appropriate answer which resolve the reprompt permission popup once initialized the page.

<body>
  <div class="wrapper">
    <section class="main-controls">
      <canvas class="visualizer" height="60px" style="display:none"></canvas>
      <div id="buttons" style="margin-bottom:20px;">
        <button class="record">Record</button>
        <button class="stop">Stop</button>
      </div>
    </section>
    <section class="sound-clips"></section>
  </div>
  <script>
    const record = document.querySelector('.record');
    const stop = document.querySelector('.stop');
    const soundClips = document.querySelector('.sound-clips');
    const mainSection = document.querySelector('.main-controls');

    stop.disabled = true;
    let audioCtx;

    if (navigator.mediaDevices.getUserMedia) {
      const constraints = {
        audio: true
      };
      let chunks = [];

      let onSuccess = function(stream) {
        const mediaRecorder = new MediaRecorder(stream);

        visualize(stream);

        record.onclick = function() {
          //ask for browser's audio record or microphone permission here
          if (navigator.mediaDevices.getUserMedia) {
            mediaRecorder.start();
            console.log("recorder started");
            record.style.background = "red";
            stop.disabled = false;
            record.disabled = true;
          }
        }

        stop.onclick = function() {
          mediaRecorder.stop();
          console.log(mediaRecorder.state);
          console.log("recorder stopped");
          record.style.background = "";
          record.style.color = "";
          stop.disabled = true;
          record.disabled = false;
        }

        mediaRecorder.onstop = function(e) {
          const clipContainer = document.createElement('article');
          const clipLabel = document.createElement('p');
          const audio = document.createElement('audio');
          clipContainer.classList.add('clip');
          audio.setAttribute('controls', '');
          clipContainer.appendChild(audio);
          clipContainer.appendChild(clipLabel);
          soundClips.appendChild(clipContainer);

          audio.controls = true;
          const blob = new Blob(chunks, {
            'type': 'audio/ogg; codecs=opus'
          });
          chunks = [];
          const audioURL = window.URL.createObjectURL(blob);
          audio.src = audioURL;
          console.log("recorder stopped");


          clipLabel.onclick = function() {
            const existingName = clipLabel.textContent;
            const newClipName = prompt('Enter a new name for your sound clip?');
            if (newClipName === null) {
              clipLabel.textContent = existingName;
            } else {
              clipLabel.textContent = newClipName;
            }
          }
        }

        mediaRecorder.ondataavailable = function(e) {
          chunks.push(e.data);
        }
      }

      let onError = function(err) {
        console.log('The following error occured: ' + err);
      }

      navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);

    } else {
      console.log('getUserMedia not supported on your browser!');
    }

    function visualize(stream) {
      if (!audioCtx) {
        audioCtx = new AudioContext();
      }

      const source = audioCtx.createMediaStreamSource(stream);

      const analyser = audioCtx.createAnalyser();
      analyser.fftSize = 2048;
      const bufferLength = analyser.frequencyBinCount;
      const dataArray = new Uint8Array(bufferLength);

      source.connect(analyser);
    }
  </script>
</body>
dontcallmedom
  • 2,420
  • 14
  • 12
ankitkanojia
  • 3,072
  • 4
  • 22
  • 35

0 Answers0