Skip to content

Commit

Permalink
WebDemo: Replace deprecated navigator.getUserMedia with
Browse files Browse the repository at this point in the history
navigator.mediaDevices.getUserMedia. This fixes getting
the microphone stream in the Safari browser.
  • Loading branch information
yodakohl committed Sep 24, 2022
1 parent b4b3254 commit 438ece9
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions examples/wasm/mic.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,7 @@ function runHotwordDetection()
load_file_from_server("alexa_v3.0.35.premium","Alexa");

setupCanvas();

if (!checkMedia()){
alert("No media");
} else {
startMedia(checkAudio);
}
startMedia(checkAudio);
}

function setupCanvas()
Expand Down Expand Up @@ -321,31 +316,24 @@ function floatTo16BitPCM(input)
return newData;
}

function checkMedia()
{
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.mediaDevices.getUserMedia;

if (navigator.getUserMedia){
return true;
} else {
return false;
}
}

function startMedia(callback)
{
navigator.getUserMedia({audio:true},
function(stream) {
startMicrophone(stream, callback);
},
function(e) {
alert("Error capturing audio");
const constraints = {
audio: true,
video: false
};
navigator.mediaDevices.getUserMedia(constraints)
.then((mediaStream) => {
startMicrophone(mediaStream, callback);
}
);
)
.catch((err) => {
alert("Cannot access microphone");
});
}


function startMicrophone(stream,callback)
{
window.audioContext = new AudioContext();
Expand Down

0 comments on commit 438ece9

Please sign in to comment.