JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button>Play</button>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
</body>
</html>
JavaScript
var bufferSize = 4096;
var audioContext = new AudioContext();
var whiteNoise = audioContext.createScriptProcessor(bufferSize, 1, 1);
whiteNoise.onaudioprocess = function(e) {
var output = e.outputBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
}
whiteNoise.connect(audioContext.destination);
var playButton = document.querySelector('button').addEventListener('click', function() {
audioContext.resume().then(() => {
console.log('Playback resumed successfully');
});
});
const constraints = window.constraints = {
audio : {
echoCancellation: {exact: true},
autoGainControl: {exact: true},
channelCount: {exact: 1},
noiseSuppression: {exact: true},
},
video: false
};
function handleSuccess(stream) {
const audioTracks = stream.getAudioTracks();
console.log('Got stream with constraints:', constraints);
console.log('Using audio device: ' + audioTracks[0].label);
stream.oninactive = function() {
console.log('Stream ended');
};
window.stream = stream; // make variable available to browser console
}
function handleError(error) {
console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}
navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError);