benz-amr-recorder
repository·master·Indexed 43 days ago
https://github.com/benzleung/benz-amr-recorderA pure frontend library for decoding, playing, recording, and encoding AMR audio files (the format used by WeChat voice messages) without requiring server support. Version 1.1.5. Based on amr.js and RecorderJs, it provides a Promise-based API for initializing audio from URLs, Blobs, or ArrayBuffers, and supports converting MP3 to AMR.
What's inside benz-amr-recorder
- This project is licensed under the MIT License.
Project Maintenance Status
masterThe project is currently in a maintenance state. The author is no longer actively involved in audio-related projects and has not performed major updates recently. The underlying core library,
amr.js, is also infrequently updated. Pull Requests (PRs) are welcome from the community.Recent Updates (2022-09-26):
- Verified compatibility with Webpack and Babel via
create-react-appscaffolding. - Added functionality to unbind events.
- Added a
destroy()method to clean up instances.
- Verified compatibility with Webpack and Babel via
How to replace audio in BenzAMRRecorder
masterYou cannot change the audio source on an existing
BenzAMRRecorderinstance. To use a different audio file, you must create a new instance usingnew BenzAMRRecorder()and initialize it with the new source.To prevent memory leaks, especially in memory-intensive applications, call
amr.destroy()on the old instance before discarding it. This clears internal AMR and PCM data and unbinds all events.Install benz-amr-recorder
masterYou can install the library via npm or include the minified JS file directly in your HTML.
Using npm:
npm install benz-amr-recorderUsing a script tag:
<script type="text/javascript" src="./BenzAMRRecorder.min.js"></script>npm install benz-amr-recorderReformat MP3 to AMR
masterIf the browser supports the MP3 format, you can use this library to reformat an MP3 file into an AMR file.
var amrFromMp3 = new BenzAMRRecorder(); amrFromMp3.initWithUrl('path/to/file.mp3').then(function() { // Download the AMR file window.location.href = window.URL.createObjectURL(amrFromMp3.getBlob()); })Record AMR audio
masterTo record audio and save it as an AMR file, initialize the recorder for recording and then start the recording process.
var amrRec = new BenzAMRRecorder(); amrRec.initWithRecord().then(function() { amrRec.startRecord(); });Play AMR audio from a local file (Blob)
masterYou can play AMR files selected by a user via an
<input type="file">element by usinginitWithBlob().<input type="file" id="amr-file" accept=".amr">var amr = new BenzAMRRecorder(); var amrFileObj = document.getElementById('amr-file'); amrFileObj.onchange = function() { amr.initWithBlob(this.files[0]).then(function() { amr.play(); }); }Play AMR audio from a URL
masterTo play an existing AMR file, initialize the recorder with the file URL and then call
play().Important: Due to browser autoplay policies (Chrome 70+, iOS), you should trigger
initWithXXX()orplay()methods within a user-initiated event likeclickortouchstart.var amr = new BenzAMRRecorder(); amr.initWithUrl('path/to/voice.amr').then(function() { amr.play(); }); amr.onEnded(function() { alert('play ended'); });Play a local AMR file from an input element
masterYou can play local files by passing a
Blobfrom an<input type="file">toinitWithBlob(blob).<input type="file" id="amr-file" accept=".amr"> <script> var amr = new BenzAMRRecorder(); var amrFileObj = document.getElementById('amr-file'); amrFileObj.onchange = function() { amr.initWithBlob(this.files[0]).then(function() { amr.play(); }); } </script>Convert MP3 to AMR
masterIf the browser natively supports MP3, you can use
initWithUrl(url)with an MP3 path to load it, then usegetBlob()to get the converted AMR file.var amrFromMp3 = new BenzAMRRecorder(); amrFromMp3.initWithUrl('path/to/file.mp3').then(function() { // 下载 amr 文件 window.location.href = window.URL.createObjectURL(amrFromMp3.getBlob()); })Record audio as an AMR file
masterTo record audio, initialize the recorder using
initWithRecord()and then callstartRecord(). Once finished, callfinishRecord()to convert the recording into an AMR format.var amrRec = new BenzAMRRecorder(); amrRec.initWithRecord().then(function() { amrRec.startRecord(); });Download the recorded AMR file
masterAfter recording or converting, you can retrieve the AMR data as a
BlobusinggetBlob()and trigger a download viawindow.URL.createObjectURL().window.location.href = window.URL.createObjectURL(amr.getBlob());