npm

audio-playerx
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

audio-playerx

Core audio player in the browser

Installation

npm install audio-playerx

or

yarn add audio-playerx

Usage

audio-playerx support two modes.

HTMLAudioElement

import Playerx, { PLAYER_STATE, PLAYER_MODE } from "audio-playerx";
 
// initialization
const player = new Playerx({
  mode: PLAYER_MODE.Audio,
  onStateChange(value) {
    console.log(value);
  }
});
 
// play your audio
player.play(resource: string);
 
// pause
player.pause();
 
// play continue
player.play();

Web Audio API

import Playerx, { PLAYER_STATE, PLAYER_MODE } from "audio-playerx";
 
// initialization
const player = new Playerx({
  mode: PLAYER_MODE.AudioContext,
  onStateChange(value) {
    console.log(value);
  }
});
 
// play your audio
player.play(buffer: ArrayBuffer);
 
// stop
player.stop();
 
// close player
player.close();

API

AudioPlayer

import { AudioPlayer } from "audio-playerx";
 
new AudioPlayer();
 
// is equal to
 
new Playerx({ mode: PLAYER_MODE.Audio });
  • Support play, pause and resume

Constructor

new ({ onStateChange?: onStateChange } = {})AudioPlayer;

onStateChange is a callback, called when the player state changes.

type onStateChange(value: PLAYER_STATE) => any;

Methods

play: (resource: string | undefined) => Promise<PLAYER_STATE | undefined>

audio play and resume.

When the parameter 'resource' is passed, it will start playing from the beginning, otherwise it is used to continue playing the paused audio.

pause: () => void

ACTXPlayer

import { ACTXPlayer } from "audio-playerx";
 
new ACTXPlayer();
 
// is equal to
 
new Playerx({ mode: PLAYER_MODE.AudioContext });
  • Only supports play, stop
  • doesn't support pause, resume

Constructor

new ({ onStateChange?: onStateChange } = {})AudioPlayer;

onStateChange is a callback, called when the player state changes.

type onStateChange(value: PLAYER_STATE) => any;

Methods

play: (buffer: ArrayBuffer) => Promise<PLAYER_STATE | undefined>,
stop: () => void
close: () => void

PLAYER_MODE

player mode enum

{
  Audio: 0,
  AudioContext: 1,
}

PLAYER_STATE

player state enum

{
  READY: 0,
  LOADING: 1,
  PLAYING: 2,
  PAUSED: 3,
  FAILED: 4,
  DESTROYED: 5,
}

Readme

Keywords

Package Sidebar

Install

npm i audio-playerx

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

32.3 kB

Total Files

13

Last publish

Collaborators

  • zousdie