Skip to content

Instantly share code, notes, and snippets.

@iSanjayAchar
Created October 23, 2018 06:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iSanjayAchar/dab0e7ae654d26dcc66460646e154965 to your computer and use it in GitHub Desktop.
Save iSanjayAchar/dab0e7ae654d26dcc66460646e154965 to your computer and use it in GitHub Desktop.
Record Audio using JavaScript and Output in Base64
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);
@pixellet14
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment