A MediaRecorder ponyfill that records audio as mp3. It uses the great Kagami/vmsg library under the hood to encode mp3 audio in WebAssembly using LAME.
- Standard MediaRecorder API
- Audio encoding off the main thread using Web Workers
- Consistent MP3 file output in all supported browsers
- High quality type definitions
- 3.7kB gzipped
- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+
Install with npm or yarn.
npm i mp3-mediarecorder
If you don't want to set up a build environment, you can get mp3-mediarecorder from a CDN like unpkg.com and it will be globally available through the window.mp3MediaRecorder object.
<script src="https://unpkg.com/mp3-mediarecorder"></script>
import { getMp3MediaRecorder } from 'mp3-mediarecorder';
getMp3MediaRecorder({ wasmURL: '/dist/vmsg.wasm' }).then(Mp3MediaRecorder => {
const recorder = new Mp3MediaRecorder(mediaStream);
recorder.start();
});
getMp3MediaRecorder returns a Promise<typeof MediaRecorder>
. The promise will resolve once the Web Assembly module is initialized and the library is ready to start recording.
đź’ˇ For more detailed API docs, check out MediaRecorder on MDN
Browser support for MediaRecorder is lacking.
Even in browsers with support for MediaRecorder, the available audio formats differ between browsers, and are not always compatible with other browsers. MP3 is the only audio format that can be played by all modern browsers.
Kagami/vmsg is a great library but I needed something that doesn't include a UI and/or getUserMedia code.
- The
dataavailable
event only fires once, when encoding is complete.MediaRecorder.start
ignores its optionaltimeSlice
argument MediaRecorder.requestData
does not trigger adataavailable
eventbitsPerSecond
is not configurable, theMediaRecorder
constructor ignores itsoptions
argument.- The module returns a Promise to avoid delaying
MediaRecorder.start
until the Web Worker is ready. Once the Promise is resolved, the Web Worker will be ready to start encoding.
- Kagami/vmsg: Use this library if you want a more complete microphone recording library with a built-in UI