0.1.8 • Published 5 years ago

ease-audio v0.1.8

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

Description

ease-audio.js is an audio library base on HTML5 Audio and Web Audio.

Quick Start

Several options to get up and running:

  • Clone the repo: git clone https://github.com/BobbyLH/ease-audio
  • Install with npm: npm install ease-audio
  • Install with Yarn: yarn add ease-audio

In the browser:

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

Examples

Most basic:

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

sound.play();

More options:

var sound = new EaseAudio({
  playlist: [
    {src: 'sound1.mp3', tag: 'your tag'}, 
    {src: 'sound2.mp3', tag: 'your tag'}
  ],
  // pick second playlist item for initiation
  initIndex: 1,
  volume: 1,
  playModel: 'list-once',
  preload: true,
  loop: true,
  autoPlay: true,
  onload: e => {
    console.log('loading')
  },
  oncanplay: e => {
    console.log('playing')
    sound.play()
  },
  onplay: e => {
    console.log('playing')
  },
  onpause: e => {
    console.log('paused')
  },
  onstop: id => {
    console.log('stopped')
  },
  onend: e => {
    console.log('ended')
  },
  onseek: e => {
    console.log('seeking')
  }
});

Listen for events:

var sound = new EaseAudio({
  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 EaseAudio from 'ease-audio';

// or const EaseAudio = require('ease-audio').default

// or const { EaseAudio } = require('ease-audio')

// Setup the new EaseAudio.
const sound = new EaseAudio({
  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.

endAutoCut bool

Set to true the EaseAudio going to play next track(according to playModel) when the current will 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):

  • list-once
  • list-random
  • list-loop
  • single-once
  • single-loop

volume Number 1.0

The volume of the specific track, from 0.0 to 1.0.

loop Boolean false

Set to true to automatically loop the sound forever.

preload Boolean true

Automatically begin downloading the audio file.

autoplay Boolean false

Set to true to automatically start playback when sound is loaded.

mute Boolean false

Set to true to load the audio muted.

rate Number 1.0

The rate of playback. 0.5 to 2.0, with 1.0 being normal speed.

onload Function

Fires when the sound is start loading.

onunload Function

Fires when the sound is start unload.

onplay Function

Fires when the sound begins playing. The first parameter is the event object.

onpause Function

Fires when the sound has been paused. The first parameter is the event object.

onstop Function

Fires when the sound has been stopped. The first parameter is the ID of the sound.

onend Function

Fires when the sound finishes playing (if it is looping, it'll fire at the end of each loop). The first parameter is the event object.

onfinish Function

Fires when the playlist has been finished. The first parameter is the ID of the sound.

onmute Function

Fires when the sound has been muted/unmuted. The first parameter is the event object.

onvolume Function

Fires when the sound's volume has changed. The first parameter is the event object.

onrate Function

Fires when the sound's playback rate has changed. The first parameter is the event object.

onseeking Function

Fires when the sound start seeking. The first parameter is the event object.

onseeked Function

Fires when the sound has been seeked. The first parameter is the event object.

oncut Function

Fires when the sound has been cutted. The first parameter is the ID of the sound.

onpick Function

Fires when the sound has been picked. The first parameter is the ID of the sound.

onloaderror Function

Fires when the sound is unable to load. The first parameter is the event object.

onplayerror Function

Fires when the sound is unable to play. The first parameter is the error message.

usingWebAudio Boolean

true if the Web Audio API is available.

debug Boolean false

Set to true will log the debug information.

logLevel string error

This property defines the level of messages that the EaseAudio will log. Valid levels include:

  • detail
  • info
  • warn
  • error
  • silent

Methods

init(config)

If without config when new EaseAudio, 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.

  • playId: Number required The playId which from playlist.

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.

  • seek: Number optional The position to move current playback to (in seconds).

rate(rate)

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

  • rate: Number optional The rate of playback. 0.5 to 2.0, with 1.0 being normal speed.

volume(volume)

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

  • volume: Number optional Volume from 0.0 to 1.0.

mute(muted)

Mutes the sound, but doesn't pause the playback.

  • muted: Boolean True to mute and false to unmute.

stop()

Stops playback of sound, resetting seek to 0.

unload()

Unload and destroy the EaseAudio 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.

  • event: String Name of event to fire/set (play, pause, stop, end, load, unload, canplay, progress, volume, seek, rate, timeupdate, loaderror, playerror).
  • function: Function Define function to fire on event.

once(event, function)

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

  • event: String Name of event to fire/set (play, pause, stop, end, load, unload, canplay, progress, volume, seek, rate, timeupdate, loaderror, playerror).
  • function: Function Define function to fire on event.

off(event, function)

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

  • event: String Name of event (play, pause, stop, end, load, unload, canplay, progress, volume, seek, rate, timeupdate, loaderror, playerror).
  • function: Function optional The listener to remove. Omit this to remove all events of type.

Properties

duration Number

Return the track duration property.

playState String

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

playId Number

Return the current playing id.

playingData Object

Return the current playing item data.

playlist Array

Return the playlist.

playlist = {action, list, playId, params}

Set the playlist according to action and playId arguments.

  • action: String Action of setting (add, delete, insert, replace, update, reset).
  • list: Array optional Add/insert/replace the list to playlist when the action is add, insert, replace.
  • playId: Array optional Delete/insert/replace/update a item according to playId.
  • params: Object optional Update a item from params according to playId.

networkState String

Return the track network state.

Browser Compatibility

  • Google Chrome 50+
  • Internet Explorer 9.0+
  • Firefox 40+
  • Safari 8+
  • Opera 50.0+
  • Microsoft Edge 12+
  • Android WebView 4+
  • Samsung 4+
  • IOS 8+

License

Copyright (c) 2018-2019 Bobby.li

Released under the MIT License

0.2.0-beta.4

5 years ago

0.2.0-beta.3

5 years ago

0.2.0-beta.2

5 years ago

0.2.0-beta.1

5 years ago

0.1.9-beta.0

5 years ago

0.1.8

5 years ago

0.1.8-beta.1

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.6-beta.1

5 years ago

0.1.6-beta.0

5 years ago

0.1.5

5 years ago

0.1.5-beta.1

5 years ago

0.1.5-beta.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.3-beta.1

5 years ago

0.1.3-beta.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.93

5 years ago

0.0.92-beta.0

5 years ago

0.0.92

5 years ago

0.0.91-beta.3

5 years ago

0.0.91-beta.2

5 years ago

0.0.91-beta.1

5 years ago

0.0.91-beta.0

5 years ago

0.0.91

5 years ago

0.0.90

5 years ago

0.0.89

5 years ago

0.0.88

5 years ago

0.0.87

5 years ago

0.0.86

5 years ago

0.0.85

5 years ago

0.0.84

5 years ago

0.0.83

5 years ago

0.0.82

5 years ago

0.0.81

5 years ago

0.0.80

5 years ago

0.0.79

5 years ago

0.0.78

5 years ago

0.0.77

5 years ago

0.0.76

5 years ago

0.0.75

5 years ago

0.0.74

5 years ago

0.0.73

5 years ago

0.0.72

5 years ago

0.0.71

5 years ago

0.0.70

5 years ago

0.0.69

5 years ago

0.0.68

5 years ago

0.0.67

5 years ago

0.0.66

5 years ago

0.0.65

5 years ago

0.0.64

5 years ago

0.0.63

5 years ago

0.0.62

5 years ago

0.0.61

5 years ago

0.0.60

5 years ago

0.0.59

5 years ago

0.0.58

5 years ago

0.0.57

5 years ago

0.0.56

5 years ago

0.0.55

5 years ago

0.0.54

5 years ago

0.0.53

5 years ago

0.0.52

5 years ago

0.0.51

5 years ago

0.0.50

5 years ago

0.0.49

5 years ago

0.0.48

5 years ago

0.0.47

5 years ago

0.0.46

5 years ago

0.0.45

5 years ago

0.0.44

5 years ago

0.0.43

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago