AMRPlayer is a modernized version of amr.js, supporting the loading and playback of AMR audio format.
amr.js has ceased updates, utilizing numerous global variables, lacking support for strict mode, not supporting modularity, and missing documentation.
This project has modernized it, supporting ES Module, CommonJS, and IIFE formats, compatible with modern build tools (such as Webpack, Rollup, Vite, etc.). It also adds playback control support and convenient data writing methods.
I used AMR audio interpretation in my project and found that there are not many libraries to choose from. Although amr.js lacks maintenance, it is still usable. Thanks to the original authors for their contributions, and thus I also share my achievements with everyone.
- Easy loading of AMR audio files.
- Provides audio playback control.
- Supports converting AMR audio to WAV format.
- Compatible with modern JavaScript build tools.
- No longer uses multiple global variables; modular environments do not require global variables, and only one global variable is exposed in the browser environment.
You can install AMRPlayer via npm:
npm install amrplayer
Depending on your project environment, you can choose any of the following methods to import AMRPlayer.
If your project uses ES Module (e.g., Vite or modern browser environments), you can import it as follows:
import AMRPlayer from 'amrplayer';
// Using AMRPlayer
const amrplayer = new AMRPlayer();
If your project uses CommonJS, you can import it as follows:
const AMRPlayer = require('amrplayer');
// Using AMRPlayer
const amrplayer = new AMRPlayer();
If you are directly using the <script>
tag in the browser, you can import it as follows:
<script src="https://unpkg.com/amrplayer/dist/amrplayer.js"></script>
<script>
// AMRPlayer will be exposed as a global variable
const amrplayer = new AMRPlayer();
</script>
import AMRPlayer from 'amrplayer';
const amrplayer = new AMRPlayer();
fetch('audio.amr')
.then(response => response.arrayBuffer())
.then(arrayBuffer => amrplayer.setBuffer(arrayBuffer));
<input type="file" name="file" id="file" accept=".amr">
const file = document.getElementById('file').files[0];
// Note that setBlob is an asynchronous interface
await amrplayer.setBlob(file);
Convenient for playback using <audio>
.
<audio id="audio" controls></audio>
const url = amrplayer.getWAV();
document.getElementById('audio').src = url;
const amraudio = amrplayer.getAudio(() => console.log('Audio playback ended'));
// Synchronous interface
amraudio.start(); // Start playback
amraudio.stop(); // Stop playback
// Asynchronous interface
amraudio.suspend(); // Pause playback
amraudio.resume(); // Resume playback
amraudio.toggle(); // Toggle playback state
amraudio.close(); // Close the audio context when amraudio is no longer needed
console.log('Current audio state:', amraudio.state);
Decodes the passed ArrayBuffer
into Float32Array
and sets it as the audio data.
arrayBuffer
: TheArrayBuffer
to decode.- Returns the decoded
Float32Array
.
Converts the passed Blob
into ArrayBuffer
and sets it as the audio data.
blob
: TheBlob
to convert.- Returns a
Promise
that resolves to the decodedFloat32Array
.
Creates an AMRAudio
instance and sets the callback function for when audio playback ends.
onEnd
: The callback function triggered when audio playback ends.- Returns an
AMRAudio
instance.
Converts the current audio data to WAV format.
- Returns a base64 string:
data:audio/wav;base64,...
Creates an AMRAudio
instance.
float32Array
: TheFloat32Array
used to create the audio buffer.
Starts audio playback.
Stops audio playback.
Closes the audio context. Asynchronous interface.
Suspends the audio context. Asynchronous interface.
Resumes the audio context. Asynchronous interface.
Toggles the audio playback state, cycling through: play -> pause -> resume. Asynchronous interface.
Gets the current audio playback state: 'stopped', 'running', 'suspended'
.
Automatically called when playback ends. Triggered by automatic end or calling AMRAudio.stop()
.
This project is open-sourced under the MIT license. For more details, please refer to the LICENSE file.