Manage browser audio sources and playback
npm install audio-director
https://lab.miguelmota.com/audio-director
- Player :
object
Class reprensenting a Player
Kind: global class
- Player
- new Player()
- instance
- .on(eventName, callback)
- .getCurrentUrl() ⇒
String
- .emptyQueue() ⇒
Promise
- .enqueue(source) ⇒
Promise
- .deque() ⇒
Promise
- .play() ⇒
Promise
- .playQueue() ⇒
Promise
- .stop() ⇒
Promise
- .pause() ⇒
Promise
- .replay() ⇒
Promise
- .playBlob(blob) ⇒
Promise
- .playAudioBuffer(audioBuffer) ⇒
Promise
- .getCurrentAudioBuffer() ⇒
AudioBuffer
- .playUrl(url) ⇒
Promise
- .getAudioDataFromUrl(url) ⇒
Promise
- .next() ⇒
Promise
- .previous() ⇒
Promise
- .setRandom(enabled) ⇒
Promise
- .setRepeat(enabled) ⇒
Promise
- .hasNext() ⇒
Boolean
- .hasPrevious() ⇒
Boolean
- .setPlaybackRate(playbackRate) ⇒
Promise
- .getPlaybackRate() ⇒
Number
- .setVolume(volume) ⇒
Promise
- .getVolume() ⇒
Number
- .setMaxVolume(maxVolume) ⇒
Promise
- .getMaxVolume() ⇒
Number
- .setMuted(enabled) ⇒
Promise
- .getCurrentTime() ⇒
Number
- .seekTo(seconds) ⇒
Promise
- .getDuration() ⇒
Number
- .getCurrentState() ⇒
String
- static
- .EventTypes ⇒
Object
- .EventTypes ⇒
Create a Player
Listen for player events
Kind: instance method of Player
Param | Type | Description |
---|---|---|
eventName | String |
name of even |
callback | function |
called when the event occurs |
Example
player.on(Player.EventTypes.PLAY, () => {
})
Example
player.on(Player.EventTypes.PLAYBACK_RATE, () => {
})
Get currently playing audio source url
Kind: instance method of Player
Example
const url = player.getCurrentUrl()
Empties queue
Kind: instance method of Player
Example
player.emptyQueue()
Enqueues an audio source to play
Kind: instance method of Player
Param | Type | Description |
---|---|---|
source | DataView | Uint8Array | AudioBuffer | ArrayBuffer | String |
an 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)
Deques an audio source from queue
Kind: instance method of Player
Example
player.deque()
Plays current audio source in queue
Kind: instance method of Player
Example
player.play()
Start playing audio sources in queue
Kind: instance method of Player
Example
player.playQueue()
Stop playing current audio source
Kind: instance method of Player
Example
player.stop()
Pause playing current audio source
Kind: instance method of Player
Example
player.pause()
Replay current audio source
Kind: instance method of Player
Example
player.replay()
Play an audio Blob
Kind: instance method of Player
Param | Type | Description |
---|---|---|
blob | Blob |
audio blob |
Example
const blob = new Blob([dataView], {
type: 'audio/wav'
})
player.playBlob(blob)
Play an AudioBuffer
Kind: instance method of Player
Param | Type | Description |
---|---|---|
audioBuffer | AudioBuffer |
an AudioBuffer |
Example
player.playAudioBuffer(audioBuffer)
Return current audio buffer playing
Kind: instance method of Player
Example
player.getCurrentAudioBuffer()
Play an MP3 url
Kind: instance method of Player
Param | Type | Description |
---|---|---|
url | String |
MP3 url |
Example
const url = 'https://example.com/audio.mp3'
player.playUrl(url)
Get the binary audio data from an audio source url in ArrayBuffer form
Kind: instance method of Player
Returns: Promise
- arrayBuffer
Param | Type | Description |
---|---|---|
url | String |
audio source url |
Example
const url = 'https://example.com/audio.mp3'
player.getAudioDataFromUrl()
.then(arrayBuffer => {
})
Play next audio source in queue
Kind: instance method of Player
Example
player.next()
Play previous audio source in queue
Kind: instance method of Player
Example
player.previous()
Enable to disable random playback
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable random playback |
Example
player.setRandom(true)
Enable to disable repeat mode. Repeat mode replays the audio sources once the entire queue has finished playing.
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable repeat mode |
Example
player.setRepeat(true)
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()
Return true if there's a previous audio source in queue
Kind: instance method of Player
Returns: Boolean
- hasPrevious
Example
const hasPrevious = player.hasPrevious()
Set the plaback rate speed, range 0.75-2
Kind: instance method of Player
Param | Type | Description |
---|---|---|
playbackRate | Number |
new playback rate |
Example
player.setPlaybackRate(2)
Get the current plaback rate speed
Kind: instance method of Player
Returns: Number
- playback rate speed
Example
const playbackRate = player.getPlaybackRate()
Set volume, range 0-1
Kind: instance method of Player
Param | Type | Description |
---|---|---|
volume | Number |
volume value |
Example
player.setVolume(0.9)
Get current volume value
Kind: instance method of Player
Returns: Number
- volume - current volume value
Example
player.getVolume()
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
Param | Type | Description |
---|---|---|
maxVolume | Number |
max volume, range 0-1 |
Example
player.setMaxVolume(0.8)
Get max volume value
Kind: instance method of Player
Returns: Number
- maxVolume - max volume value
Example
player.getMaxVolume()
Set volume level to muted
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable mute |
Example
player.setMuted(true)
Return elapsed time in seconds since the audio source started playing
Kind: instance method of Player
Returns: Number
- time - current time
Example
player.getCurrentTime()
Seek to a specified time in audio source
Kind: instance method of Player
Param | Type | Description |
---|---|---|
seconds | Number |
number of seconds to seek to |
Example
const seconds = 45
player.seekTo(seconds)
Get duration of audio source
Kind: instance method of Player
Returns: Number
- duration - duration of audio source
Example
player.getDuration()
Get current state of player
Kind: instance method of Player
Returns: String
- - current state
Example
const currentState = player.getCurrentState()
console.log(currentState) // 'playing'
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'
}
Class reprensenting a Player
Kind: global namespace Example
const player = new Player()
- Player :
object
- new Player()
- instance
- .on(eventName, callback)
- .getCurrentUrl() ⇒
String
- .emptyQueue() ⇒
Promise
- .enqueue(source) ⇒
Promise
- .deque() ⇒
Promise
- .play() ⇒
Promise
- .playQueue() ⇒
Promise
- .stop() ⇒
Promise
- .pause() ⇒
Promise
- .replay() ⇒
Promise
- .playBlob(blob) ⇒
Promise
- .playAudioBuffer(audioBuffer) ⇒
Promise
- .getCurrentAudioBuffer() ⇒
AudioBuffer
- .playUrl(url) ⇒
Promise
- .getAudioDataFromUrl(url) ⇒
Promise
- .next() ⇒
Promise
- .previous() ⇒
Promise
- .setRandom(enabled) ⇒
Promise
- .setRepeat(enabled) ⇒
Promise
- .hasNext() ⇒
Boolean
- .hasPrevious() ⇒
Boolean
- .setPlaybackRate(playbackRate) ⇒
Promise
- .getPlaybackRate() ⇒
Number
- .setVolume(volume) ⇒
Promise
- .getVolume() ⇒
Number
- .setMaxVolume(maxVolume) ⇒
Promise
- .getMaxVolume() ⇒
Number
- .setMuted(enabled) ⇒
Promise
- .getCurrentTime() ⇒
Number
- .seekTo(seconds) ⇒
Promise
- .getDuration() ⇒
Number
- .getCurrentState() ⇒
String
- static
- .EventTypes ⇒
Object
- .EventTypes ⇒
Create a Player
Listen for player events
Kind: instance method of Player
Param | Type | Description |
---|---|---|
eventName | String |
name of even |
callback | function |
called when the event occurs |
Example
player.on(Player.EventTypes.PLAY, () => {
})
Example
player.on(Player.EventTypes.PLAYBACK_RATE, () => {
})
Get currently playing audio source url
Kind: instance method of Player
Example
const url = player.getCurrentUrl()
Empties queue
Kind: instance method of Player
Example
player.emptyQueue()
Enqueues an audio source to play
Kind: instance method of Player
Param | Type | Description |
---|---|---|
source | DataView | Uint8Array | AudioBuffer | ArrayBuffer | String |
an 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)
Deques an audio source from queue
Kind: instance method of Player
Example
player.deque()
Plays current audio source in queue
Kind: instance method of Player
Example
player.play()
Start playing audio sources in queue
Kind: instance method of Player
Example
player.playQueue()
Stop playing current audio source
Kind: instance method of Player
Example
player.stop()
Pause playing current audio source
Kind: instance method of Player
Example
player.pause()
Replay current audio source
Kind: instance method of Player
Example
player.replay()
Play an audio Blob
Kind: instance method of Player
Param | Type | Description |
---|---|---|
blob | Blob |
audio blob |
Example
const blob = new Blob([dataView], {
type: 'audio/wav'
})
player.playBlob(blob)
Play an AudioBuffer
Kind: instance method of Player
Param | Type | Description |
---|---|---|
audioBuffer | AudioBuffer |
an AudioBuffer |
Example
player.playAudioBuffer(audioBuffer)
Return current audio buffer playing
Kind: instance method of Player
Example
player.getCurrentAudioBuffer()
Play an MP3 url
Kind: instance method of Player
Param | Type | Description |
---|---|---|
url | String |
MP3 url |
Example
const url = 'https://example.com/audio.mp3'
player.playUrl(url)
Get the binary audio data from an audio source url in ArrayBuffer form
Kind: instance method of Player
Returns: Promise
- arrayBuffer
Param | Type | Description |
---|---|---|
url | String |
audio source url |
Example
const url = 'https://example.com/audio.mp3'
player.getAudioDataFromUrl()
.then(arrayBuffer => {
})
Play next audio source in queue
Kind: instance method of Player
Example
player.next()
Play previous audio source in queue
Kind: instance method of Player
Example
player.previous()
Enable to disable random playback
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable random playback |
Example
player.setRandom(true)
Enable to disable repeat mode. Repeat mode replays the audio sources once the entire queue has finished playing.
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable repeat mode |
Example
player.setRepeat(true)
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()
Return true if there's a previous audio source in queue
Kind: instance method of Player
Returns: Boolean
- hasPrevious
Example
const hasPrevious = player.hasPrevious()
Set the plaback rate speed, range 0.75-2
Kind: instance method of Player
Param | Type | Description |
---|---|---|
playbackRate | Number |
new playback rate |
Example
player.setPlaybackRate(2)
Get the current plaback rate speed
Kind: instance method of Player
Returns: Number
- playback rate speed
Example
const playbackRate = player.getPlaybackRate()
Set volume, range 0-1
Kind: instance method of Player
Param | Type | Description |
---|---|---|
volume | Number |
volume value |
Example
player.setVolume(0.9)
Get current volume value
Kind: instance method of Player
Returns: Number
- volume - current volume value
Example
player.getVolume()
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
Param | Type | Description |
---|---|---|
maxVolume | Number |
max volume, range 0-1 |
Example
player.setMaxVolume(0.8)
Get max volume value
Kind: instance method of Player
Returns: Number
- maxVolume - max volume value
Example
player.getMaxVolume()
Set volume level to muted
Kind: instance method of Player
Param | Type | Description |
---|---|---|
enabled | Boolean |
boolean to enable mute |
Example
player.setMuted(true)
Return elapsed time in seconds since the audio source started playing
Kind: instance method of Player
Returns: Number
- time - current time
Example
player.getCurrentTime()
Seek to a specified time in audio source
Kind: instance method of Player
Param | Type | Description |
---|---|---|
seconds | Number |
number of seconds to seek to |
Example
const seconds = 45
player.seekTo(seconds)
Get duration of audio source
Kind: instance method of Player
Returns: Number
- duration - duration of audio source
Example
player.getDuration()
Get current state of player
Kind: instance method of Player
Returns: String
- - current state
Example
const currentState = player.getCurrentState()
console.log(currentState) // 'PLAYING'
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'
}
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()
})
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())