I'm using a navigator.mediaDevices.getUserMedia({ audio: true })
to get the user microphone. This is my code:
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
mediaRecorder.addEventListener("stop", () => {
blob = new Blob(audioChunks);
audioUrl = URL.createObjectURL(blob);
});
});
Then when the user clicks the stop button, it does this:
mediaRecorder.stop();
addSound(audioUrl);
Then addSound
function creates an object with the audio and some other info and puts it into an array. Then, I have a loop that works as a sequencer, this is the code:
function loop(){
secondTimer += (1/40);
for(var i = 0; i < audios.length; i++){
var audio = audios[i];
var start = audio.start;
if(!audio.played && secondTimer >= start){
audio.audio.play();
audio.played = true;
}
}
if(play == true){
setTimeout(loop, 25);
}
}
Basically it checks if the current time from the sequencer is greater or equal than the start time from the audio. If it is, then it plays the audio and set its "played" property to true so it can't be played twice.
The problem is, the audio I recorded with the mic is not playing. However, if I create the audio object using an mp3 file for example, it works.
Another thing to note is that if I do audio.play()
on the addSound
function it works. Also, when I go to chrome dev tools and try to do audios[x].audio.play()
it also works. So it's only not working when it's called from the loop function, which is weird.
This is the source from the audio element shown when console logging it:
blob:http://localhost/4a7bb4a3-1940-496c-a545-a956d1ccbd57
If its any help.
Thanks in advance!