0.0.20 • Published 5 years ago

audio-director v0.0.20

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

audio-director

Manage browser audio sources and playback

License Build Status dependencies Status NPM version

Install

npm install audio-director

Documentation

https://lab.miguelmota.com/audio-director

Classes

Objects

Player

Kind: global class

new Player()

Create a Player

player.on(eventName, callback)

Listen for player events

Kind: instance method of Player

ParamTypeDescription
eventNameStringname of even
callbackfunctioncalled when the event occurs

Example

player.on(Player.EventTypes.PLAY, () => {

})

Example

player.on(Player.EventTypes.PLAYBACK_RATE, () => {

})

player.getCurrentUrl() ⇒ String

Get currently playing audio source url

Kind: instance method of Player Example

const url = player.getCurrentUrl()

player.emptyQueue() ⇒ Promise

Empties queue

Kind: instance method of Player Example

player.emptyQueue()

player.enqueue(source) ⇒ Promise

Enqueues an audio source to play

Kind: instance method of Player

ParamTypeDescription
sourceDataView | Uint8Array | AudioBuffer | ArrayBuffer | Stringan audio source to play

Example

const url = 'https://example.com/audio.mp3'

player.enqueue(url)

Example

player.enqueue(audioBuffer)

Example

player.enqueue(arrayBuffer)

Example

player.enqueue(blob)

player.deque() ⇒ Promise

Deques an audio source from queue

Kind: instance method of Player Example

player.deque()

player.play() ⇒ Promise

Plays current audio source in queue

Kind: instance method of Player Example

player.play()

player.playQueue() ⇒ Promise

Start playing audio sources in queue

Kind: instance method of Player Example

player.playQueue()

player.stop() ⇒ Promise

Stop playing current audio source

Kind: instance method of Player Example

player.stop()

player.pause() ⇒ Promise

Pause playing current audio source

Kind: instance method of Player Example

player.pause()

player.replay() ⇒ Promise

Replay current audio source

Kind: instance method of Player Example

player.replay()

player.playBlob(blob) ⇒ Promise

Play an audio Blob

Kind: instance method of Player

ParamTypeDescription
blobBlobaudio blob

Example

const blob = new Blob([dataView], {
  type: 'audio/wav'
})

player.playBlob(blob)

player.playAudioBuffer(audioBuffer) ⇒ Promise

Play an AudioBuffer

Kind: instance method of Player

ParamTypeDescription
audioBufferAudioBufferan AudioBuffer

Example

player.playAudioBuffer(audioBuffer)

player.getCurrentAudioBuffer() ⇒ AudioBuffer

Return current audio buffer playing

Kind: instance method of Player Example

player.getCurrentAudioBuffer()

player.playUrl(url) ⇒ Promise

Play an MP3 url

Kind: instance method of Player

ParamTypeDescription
urlStringMP3 url

Example

const url = 'https://example.com/audio.mp3'

player.playUrl(url)

player.getAudioDataFromUrl(url) ⇒ Promise

Get the binary audio data from an audio source url in ArrayBuffer form

Kind: instance method of Player Returns: Promise - arrayBuffer

ParamTypeDescription
urlStringaudio source url

Example

const url = 'https://example.com/audio.mp3'

player.getAudioDataFromUrl()
.then(arrayBuffer => {

})

player.next() ⇒ Promise

Play next audio source in queue

Kind: instance method of Player Example

player.next()

player.previous() ⇒ Promise

Play previous audio source in queue

Kind: instance method of Player Example

player.previous()

player.setRandom(enabled) ⇒ Promise

Enable to disable random playback

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable random playback

Example

player.setRandom(true)

player.setRepeat(enabled) ⇒ Promise

Enable to disable repeat mode. Repeat mode replays the audio sources once the entire queue has finished playing.

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable repeat mode

Example

player.setRepeat(true)

player.hasNext() ⇒ Boolean

Return true if there's an audio source to play next in queue

Kind: instance method of Player Returns: Boolean - hasNext Example

const hasNext = player.hasNext()

player.hasPrevious() ⇒ Boolean

Return true if there's a previous audio source in queue

Kind: instance method of Player Returns: Boolean - hasPrevious Example

const hasPrevious = player.hasPrevious()

player.setPlaybackRate(playbackRate) ⇒ Promise

Set the plaback rate speed, range 0.75-2

Kind: instance method of Player

ParamTypeDescription
playbackRateNumbernew playback rate

Example

player.setPlaybackRate(2)

player.getPlaybackRate() ⇒ Number

Get the current plaback rate speed

Kind: instance method of Player Returns: Number - playback rate speed Example

const playbackRate = player.getPlaybackRate()

player.setVolume(volume) ⇒ Promise

Set volume, range 0-1

Kind: instance method of Player

ParamTypeDescription
volumeNumbervolume value

Example

player.setVolume(0.9)

player.getVolume() ⇒ Number

Get current volume value

Kind: instance method of Player Returns: Number - volume - current volume value Example

player.getVolume()

player.setMaxVolume(maxVolume) ⇒ Promise

Set the maximum volume limit. For example if max volume is set to 0.6, then when volume is scaled from 0-0.6, meaning that volume level at 1 will play at 0.6

Kind: instance method of Player

ParamTypeDescription
maxVolumeNumbermax volume, range 0-1

Example

player.setMaxVolume(0.8)

player.getMaxVolume() ⇒ Number

Get max volume value

Kind: instance method of Player Returns: Number - maxVolume - max volume value Example

player.getMaxVolume()

player.setMuted(enabled) ⇒ Promise

Set volume level to muted

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable mute

Example

player.setMuted(true)

player.getCurrentTime() ⇒ Number

Return elapsed time in seconds since the audio source started playing

Kind: instance method of Player Returns: Number - time - current time Example

player.getCurrentTime()

player.seekTo(seconds) ⇒ Promise

Seek to a specified time in audio source

Kind: instance method of Player

ParamTypeDescription
secondsNumbernumber of seconds to seek to

Example

const seconds = 45

player.seekTo(seconds)

player.getDuration() ⇒ Number

Get duration of audio source

Kind: instance method of Player Returns: Number - duration - duration of audio source Example

player.getDuration()

player.getCurrentState() ⇒ String

Get current state of player

Kind: instance method of Player Returns: String - - current state Example

const currentState = player.getCurrentState()

console.log(currentState) // 'playing'

Player.EventTypes ⇒ Object

Return event types

Kind: static property of Player Returns: Object - eventTypes - all player event types Example

const EventTypes = Player.EventTypes

{
      LOG: 'log',
      ERROR: 'error',
      READY: 'ready',
      PLAY: 'play',
      REPLAY: 'replay',
      PAUSE: 'pause',
      STOP: 'pause',
      NEXT: 'next',
      PREVIOUS: 'previous',
      RANDOM: 'random',
      REPEAT: 'repeat',
      PLAYBACK_RATE: 'playbackRate',
      VOLUME: 'volume',
      MAX_VOLUME: 'maxVolume',
      MUTED: 'muted',
      ENDED: 'ended',
      ENQUEUE: 'enqueue',
      DEQUE: 'deque',
      EMPTY_QUEUE: 'emptyQueue',
      STATE_CHANGE: 'stateChange'
}

Player : object

Class reprensenting a Player

Kind: global namespace Example

const player = new Player()

new Player()

Create a Player

player.on(eventName, callback)

Listen for player events

Kind: instance method of Player

ParamTypeDescription
eventNameStringname of even
callbackfunctioncalled when the event occurs

Example

player.on(Player.EventTypes.PLAY, () => {

})

Example

player.on(Player.EventTypes.PLAYBACK_RATE, () => {

})

player.getCurrentUrl() ⇒ String

Get currently playing audio source url

Kind: instance method of Player Example

const url = player.getCurrentUrl()

player.emptyQueue() ⇒ Promise

Empties queue

Kind: instance method of Player Example

player.emptyQueue()

player.enqueue(source) ⇒ Promise

Enqueues an audio source to play

Kind: instance method of Player

ParamTypeDescription
sourceDataView | Uint8Array | AudioBuffer | ArrayBuffer | Stringan audio source to play

Example

const url = 'https://example.com/audio.mp3'

player.enqueue(url)

Example

player.enqueue(audioBuffer)

Example

player.enqueue(arrayBuffer)

Example

player.enqueue(blob)

player.deque() ⇒ Promise

Deques an audio source from queue

Kind: instance method of Player Example

player.deque()

player.play() ⇒ Promise

Plays current audio source in queue

Kind: instance method of Player Example

player.play()

player.playQueue() ⇒ Promise

Start playing audio sources in queue

Kind: instance method of Player Example

player.playQueue()

player.stop() ⇒ Promise

Stop playing current audio source

Kind: instance method of Player Example

player.stop()

player.pause() ⇒ Promise

Pause playing current audio source

Kind: instance method of Player Example

player.pause()

player.replay() ⇒ Promise

Replay current audio source

Kind: instance method of Player Example

player.replay()

player.playBlob(blob) ⇒ Promise

Play an audio Blob

Kind: instance method of Player

ParamTypeDescription
blobBlobaudio blob

Example

const blob = new Blob([dataView], {
  type: 'audio/wav'
})

player.playBlob(blob)

player.playAudioBuffer(audioBuffer) ⇒ Promise

Play an AudioBuffer

Kind: instance method of Player

ParamTypeDescription
audioBufferAudioBufferan AudioBuffer

Example

player.playAudioBuffer(audioBuffer)

player.getCurrentAudioBuffer() ⇒ AudioBuffer

Return current audio buffer playing

Kind: instance method of Player Example

player.getCurrentAudioBuffer()

player.playUrl(url) ⇒ Promise

Play an MP3 url

Kind: instance method of Player

ParamTypeDescription
urlStringMP3 url

Example

const url = 'https://example.com/audio.mp3'

player.playUrl(url)

player.getAudioDataFromUrl(url) ⇒ Promise

Get the binary audio data from an audio source url in ArrayBuffer form

Kind: instance method of Player Returns: Promise - arrayBuffer

ParamTypeDescription
urlStringaudio source url

Example

const url = 'https://example.com/audio.mp3'

player.getAudioDataFromUrl()
.then(arrayBuffer => {

})

player.next() ⇒ Promise

Play next audio source in queue

Kind: instance method of Player Example

player.next()

player.previous() ⇒ Promise

Play previous audio source in queue

Kind: instance method of Player Example

player.previous()

player.setRandom(enabled) ⇒ Promise

Enable to disable random playback

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable random playback

Example

player.setRandom(true)

player.setRepeat(enabled) ⇒ Promise

Enable to disable repeat mode. Repeat mode replays the audio sources once the entire queue has finished playing.

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable repeat mode

Example

player.setRepeat(true)

player.hasNext() ⇒ Boolean

Return true if there's an audio source to play next in queue

Kind: instance method of Player Returns: Boolean - hasNext Example

const hasNext = player.hasNext()

player.hasPrevious() ⇒ Boolean

Return true if there's a previous audio source in queue

Kind: instance method of Player Returns: Boolean - hasPrevious Example

const hasPrevious = player.hasPrevious()

player.setPlaybackRate(playbackRate) ⇒ Promise

Set the plaback rate speed, range 0.75-2

Kind: instance method of Player

ParamTypeDescription
playbackRateNumbernew playback rate

Example

player.setPlaybackRate(2)

player.getPlaybackRate() ⇒ Number

Get the current plaback rate speed

Kind: instance method of Player Returns: Number - playback rate speed Example

const playbackRate = player.getPlaybackRate()

player.setVolume(volume) ⇒ Promise

Set volume, range 0-1

Kind: instance method of Player

ParamTypeDescription
volumeNumbervolume value

Example

player.setVolume(0.9)

player.getVolume() ⇒ Number

Get current volume value

Kind: instance method of Player Returns: Number - volume - current volume value Example

player.getVolume()

player.setMaxVolume(maxVolume) ⇒ Promise

Set the maximum volume limit. For example if max volume is set to 0.6, then when volume is scaled from 0-0.6, meaning that volume level at 1 will play at 0.6

Kind: instance method of Player

ParamTypeDescription
maxVolumeNumbermax volume, range 0-1

Example

player.setMaxVolume(0.8)

player.getMaxVolume() ⇒ Number

Get max volume value

Kind: instance method of Player Returns: Number - maxVolume - max volume value Example

player.getMaxVolume()

player.setMuted(enabled) ⇒ Promise

Set volume level to muted

Kind: instance method of Player

ParamTypeDescription
enabledBooleanboolean to enable mute

Example

player.setMuted(true)

player.getCurrentTime() ⇒ Number

Return elapsed time in seconds since the audio source started playing

Kind: instance method of Player Returns: Number - time - current time Example

player.getCurrentTime()

player.seekTo(seconds) ⇒ Promise

Seek to a specified time in audio source

Kind: instance method of Player

ParamTypeDescription
secondsNumbernumber of seconds to seek to

Example

const seconds = 45

player.seekTo(seconds)

player.getDuration() ⇒ Number

Get duration of audio source

Kind: instance method of Player Returns: Number - duration - duration of audio source Example

player.getDuration()

player.getCurrentState() ⇒ String

Get current state of player

Kind: instance method of Player Returns: String - - current state Example

const currentState = player.getCurrentState()

console.log(currentState) // 'PLAYING'

Player.EventTypes ⇒ Object

Return event types

Kind: static property of Player Returns: Object - eventTypes - all player event types Example

const EventTypes = Player.EventTypes

{
      LOG: 'log',
      ERROR: 'error',
      READY: 'ready',
      PLAY: 'play',
      REPLAY: 'replay',
      PAUSE: 'pause',
      STOP: 'pause',
      NEXT: 'next',
      PREVIOUS: 'previous',
      RANDOM: 'random',
      REPEAT: 'repeat',
      PLAYBACK_RATE: 'playbackRate',
      VOLUME: 'volume',
      MAX_VOLUME: 'maxVolume',
      MUTED: 'muted',
      ENDED: 'ended',
      ENQUEUE: 'enqueue',
      DEQUE: 'deque',
      EMPTY_QUEUE: 'emptyQueue',
      STATE_CHANGE: 'stateChange'
}

Usage

Standard audio player

const {Player} = require('audio-director')
const player = new Player()

// add audio source(s) to play queue. Converts input to AudioBuffer.
player.enqueue(dataView|typedArray|arrayBuffer|url)
.then(audioBuffer => {
  player.play()
})

Full documentation

YouTube player

const {YoutubePlayer} = require('audio-director')
const player = new YoutubePlayer()

const url = 'https://www.youtube.com/watch?v=_5joTyy3CCo&list=RDQMc4l8l2aQrNo'

player.enqueue(url)
.then(() => player.play())

Full documentation

Example

Live basic example

Source

https://github.com/miguelmota/audio-director

License

MIT

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago