View on GitHub

roar

Lighter javascript audio library https://bobbylh.github.io/roar/.

Description

NPM downloads npm version Build Status codecov install size license

roar.js is an audio library base on HTML5 Audio.

Quick Start

Several options to get up and running:

In the browser:

<script src='pathTo/dist/roar.js' ></script>
<script>
    const Roar = window.Roar.default
    var sound = new Roar({
      playlist: ['sound.mp3']
    });
</script>

Examples

Most basic:

var sound = new Roar({
  playlist: ['sound.mp3', 'sound2.mp3'],
  autocut: true
});

sound.play();

More options:

var sound = new Roar({
  playlist: [
    {src: 'sound1.mp3', tag: 'your tag'}, 
    {src: 'sound2.mp3', tag: 'your tag'}
  ],
  // pick second playlist item for initiation
  initIndex: 1,
  autocut: (currentId, nextId) => {
    // handle async
    return new Promise((resovle, reject) => {
      // do something, such as http(s) request
      resovle(true)
    })
  }
  volume: 1,
  playModel: 'list-once',
  preload: true,
  loop: true,
  autoPlay: true,
  onload: e => {
    console.log('loading')
  },
  oncanplay: e => {
    console.log('can play')
    sound.play()
  },
  onplay: e => {
    console.log('playing')
  },
  onpause: e => {
    console.log('paused')
  },
  onstop: id => {
    console.log('stopped')
  },
  onend: e => {
    console.log('ended')
  },
  onseeking: e => {
    console.log('seeking')
  }
});

Listen for events:

var sound = new Roar({
  playlist: ['sound.mp3']
});

// Clear listener after first call onload event.
sound.once('load', function(){
  console.log('loading')
});

// Fires when the sound finishes playing.
sound.on('end', function(){
  console.log('ended!');
});

ES6:

import Roar from 'roarjs';

// or const Roar = require('roarjs').default

// or const { Roar } = require('roarjs')

// Setup the new Roar.
const sound = new Roar({
  playlist: ['sound.mp3']
});

// load the sound.
sound.load();

// Play the sound.
sound.play();

// Change the volume.
sound.volume(0.5)

API

Options

playlist Array [] required

The play list for list model, the src(The sources to the track to be loaded for the sound) property is required.

initIndex Number 0

The index in playlist which will be picked to initialize

autocut Boolean or Function(currentId, nextId)

Set or return(Function) to true the Roar going to play next track(according to playModel) when the current has have finished.

playModel String list-once

This property defines the play model that when cut or end auto cut sound will be comply. Valid levels include(If set the loop to true without set playModel property, then the play model will be single-once):

Methods

init(config)

If without config when new the construction of Roar, you still can call this method with some configurations to construction.

play()

Begin playback of sound.

pause()

Pause playback of sound.

toggle()

Auto switch to play or pause according current play state.

cut()

According to play model auto cut playback for sound.

pick(playId)

According to play id pick playback for sound.This method necessarily takes 1 arguments.

load()

If you set preload to false, you must call load before you can play any sounds.

seek([seek])

Get/set the position of playback for sound. This method optionally takes 0 or 1 arguments.

rate([rate])

Get/set the rate of playback for sound. This method optionally takes 0 or 1 arguments.

volume([volume])

Get/set volume of sound. This method optionally takes 0 or 1 arguments.

mute(muted)

Mutes the sound, but doesn’t pause the playback. If without arguments then return boolean which represent whether or not muted

stop()

Stops playback of sound, resetting seek to 0.

unload()

Unload and destroy the Roar object. This will immediately stop and remove it from the cache.

on(event, function)

Listen for events. Multiple events can be added by calling this multiple times.

once(event, function)

Same as on, but it removes itself after the callback is fired.

off(event, [function])

Remove event listener that you’ve set. Call without parameters to remove all events.

model([model])

Get/set the play model. This method optionally takes 0 or 1 arguments.

playlist([options])

Get/set the playlist. This method optionally takes 0 or 1 arguments.

Properties

duration Number ReadOnly

Return the track duration property.

playState String ReadOnly

Return the play state(loading, loaded, playing, paused, stopped, ended, finished, loaderror, playerror, unloaded).

playId Number ReadOnly

Return the current playing id.

playingData Object ReadOnly

Return the current playing item data.

networkState String ReadOnly

Return the track network state.

Browser Compatibility

License

Copyright (c) 2018 Bobby.li

Released under the MIT License