Created
October 23, 2018 06:21
-
-
Save iSanjayAchar/dab0e7ae654d26dcc66460646e154965 to your computer and use it in GitHub Desktop.
Record Audio using JavaScript and Output in Base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let recorder = null; | |
const onsuccess = (stream) => { | |
recorder = new MediaRecorder(stream, { | |
type: 'audio/ogg; codecs=opus' | |
}); | |
recorder.start(); // Starting the record | |
recorder.ondataavailable = (e) => { | |
// Converting audio blob to base64 | |
let reader = new FileReader() | |
reader.onloadend = () => { | |
console.log(reader.result); | |
// You can upload the base64 to server here. | |
} | |
reader.readAsDataURL(e.data); | |
} | |
} | |
navigator.getUserMedia = ( | |
navigator.getUserMedia || | |
navigator.webkitGetUserMedia || | |
navigator.mozGetUserMedia || | |
navigator.msGetUserMedia | |
); | |
navigator.getUserMedia({ | |
audio: true | |
}, onsuccess, (e) => { | |
console.log(e); | |
}); | |
setTimeout(() => { | |
recorder.stop(); // Stopping the recorder after 3 seconds | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you so much for sharing this code. Is there any way we can increase the sound quality? or change the audio codec? and format? please guide