benz-amr-recorder

repository·master·Indexed 43 days ago

https://github.com/benzleung/benz-amr-recorder

A 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.

Tokens
3.3K
Snippets
15
Records
22
Agent score
6%

What's inside benz-amr-recorder

  1. Project Maintenance Status

    master

    The 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-app scaffolding.
    • Added functionality to unbind events.
    • Added a destroy() method to clean up instances.
  2. How to replace audio in BenzAMRRecorder

    master

    You cannot change the audio source on an existing BenzAMRRecorder instance. To use a different audio file, you must create a new instance using new 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.

  3. Install benz-amr-recorder

    master

    You can install the library via npm or include the minified JS file directly in your HTML.

    Using npm:

    npm install benz-amr-recorder

    Using a script tag:

    <script type="text/javascript" src="./BenzAMRRecorder.min.js"></script>
    npm install benz-amr-recorder
  4. Reformat MP3 to AMR

    master

    If 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());
    })
  5. Play AMR audio from a local file (Blob)

    master

    You can play AMR files selected by a user via an <input type="file"> element by using initWithBlob().

    <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();
      });
    }
  6. Play AMR audio from a URL

    master

    To 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() or play() methods within a user-initiated event like click or touchstart.

    var amr = new BenzAMRRecorder();
    amr.initWithUrl('path/to/voice.amr').then(function() {
      amr.play();
    });
    
    amr.onEnded(function() {
      alert('play ended');
    });
  7. Play a local AMR file from an input element

    master

    You can play local files by passing a Blob from an <input type="file"> to initWithBlob(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>
  8. Convert MP3 to AMR

    master

    If the browser natively supports MP3, you can use initWithUrl(url) with an MP3 path to load it, then use getBlob() 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());
    })
  9. Record audio as an AMR file

    master

    To record audio, initialize the recorder using initWithRecord() and then call startRecord(). Once finished, call finishRecord() to convert the recording into an AMR format.

    var amrRec = new BenzAMRRecorder();
    amrRec.initWithRecord().then(function() {
      amrRec.startRecord();
    });