Easily record microphone on all devices
GreggHume
Posted on August 24, 2022
I have had an array of issues with mediaRecorder:
- Audio doesnt play on all devices or browsers
- Audio isnt in a standard format
- Audio is blank on some devices like ios
This package saved my life! No data chunks, no converting of files or types, it does it for you:
https://github.com/closeio/mic-recorder-to-mp3
Simple code example:
const MicRecorder = require('mic-recorder-to-mp3');
// New instance
const recorder = new MicRecorder({
bitRate: 128
});
// Start recording. Browser will request permission to use your microphone.
recorder.start().then(() => {
// update ui or whatever
}).catch((e) => {
console.error(e);
});
// Once you are done singing your best song, stop and get the mp3.
recorder.stop().getMp3().then(([buffer, blob]) => {
// do what ever you want with buffer and blob
// Example: Create a mp3 file and play
const file = new File(buffer, 'me-at-thevoice.mp3', {
type: blob.type,
lastModified: Date.now()
});
const player = new Audio(URL.createObjectURL(file));
player.play();
}).catch((e) => {
alert('We could not retrieve your message');
console.log(e);
});
Beats working with mediaRecorder directly. Thanks closeio, you make us devs look like we know what we are doing.
💖 💪 🙅 🚩
GreggHume
Posted on August 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.