AMR audio codec is an audio compression format optimized for speech coding. Since AMR is not a supported web audio format, this library provides an implementation of AMR for use in the web.
Note: Currently this library only supports narrowband AMR.
# Using npm
npm install web-amr
# Using yarn
yarn add web-amr
# Using pnpm
pnpm add web-amr
import { AMRPlayer } from "web-amr";
const player = AMRPlayer(buffer); // pass file as ArrayBuffer
If the buffer failed to be decoded, player.error
will be set. You can check for player.error?.message
before proceeding.
The returned player
instance implements a subset of the HTMLMediaElement
API. Hence, you can use it as a drop-in for a real HTML5 audio player. It might be that this subset of APIs is all that you need from both; in that case, it's safe to do this:
import type { Player } from "web-amr";
// assume we have isAMR, audioElement, file,
// and a function "read" that returns ArrayBuffer
if (!isAMR) audioElement.src = file;
else audioElement.remove();
const player: Player = isAMR ? AMRPlayer(read(file)) : audioElement;
// use player normally
player.play();
Appends an event listener for events. The callback argument sets the callback that will be invoked when the event is dispatched.
Removes the event listener in target's event listener list with the same type and callback.
Loads and starts playback of the AMR audio.
Pauses the current playback.
Sets the current playback position, in seconds.
Returns the duration in seconds of the current media resource. A NaN value is returned if duration is not available.
Gets or sets the current playback position, in seconds.
Gets a flag that specifies whether playback is paused.
Gets information about whether the playback has ended or not.
Returns an object representing the current error state of the audio.
Use pnpm
as your package manager while installing dependencies. Make sure pnpm build
runs without an error before raising a PR.
This project uses an emscripten build of OpenCORE-AMR from yxl.