项目作者: miguelmota

项目描述 :
Manage browser audio sources and playback.
高级语言: JavaScript
项目地址: git://github.com/miguelmota/audio-director.git
创建时间: 2017-06-17T20:33:16Z
项目社区:https://github.com/miguelmota/audio-director

开源协议:MIT License

下载





logo






audio-director

Manage browser audio sources and playback

License Build Status dependencies Status NPM version

Install

  1. npm install audio-director

Documentation

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

Classes


Player


Objects


Player : object

Class reprensenting a Player



Player

Kind: global class

new Player()

Create a Player

player.on(eventName, callback)

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

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

Example

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

player.getCurrentUrl() ⇒ String

Get currently playing audio source url

Kind: instance method of Player
Example

  1. const url = player.getCurrentUrl()

player.emptyQueue() ⇒ Promise

Empties queue

Kind: instance method of Player
Example

  1. player.emptyQueue()

player.enqueue(source) ⇒ Promise

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

  1. const url = 'https://example.com/audio.mp3'
  2. player.enqueue(url)

Example

  1. player.enqueue(audioBuffer)

Example

  1. player.enqueue(arrayBuffer)

Example

  1. player.enqueue(blob)

player.deque() ⇒ Promise

Deques an audio source from queue

Kind: instance method of Player
Example

  1. player.deque()

player.play() ⇒ Promise

Plays current audio source in queue

Kind: instance method of Player
Example

  1. player.play()

player.playQueue() ⇒ Promise

Start playing audio sources in queue

Kind: instance method of Player
Example

  1. player.playQueue()

player.stop() ⇒ Promise

Stop playing current audio source

Kind: instance method of Player
Example

  1. player.stop()

player.pause() ⇒ Promise

Pause playing current audio source

Kind: instance method of Player
Example

  1. player.pause()

player.replay() ⇒ Promise

Replay current audio source

Kind: instance method of Player
Example

  1. player.replay()

player.playBlob(blob) ⇒ Promise

Play an audio Blob

Kind: instance method of Player

Param Type Description
blob Blob audio blob

Example

  1. const blob = new Blob([dataView], {
  2. type: 'audio/wav'
  3. })
  4. player.playBlob(blob)

player.playAudioBuffer(audioBuffer) ⇒ Promise

Play an AudioBuffer

Kind: instance method of Player

Param Type Description
audioBuffer AudioBuffer an AudioBuffer

Example

  1. player.playAudioBuffer(audioBuffer)

player.getCurrentAudioBuffer() ⇒ AudioBuffer

Return current audio buffer playing

Kind: instance method of Player
Example

  1. player.getCurrentAudioBuffer()

player.playUrl(url) ⇒ Promise

Play an MP3 url

Kind: instance method of Player

Param Type Description
url String MP3 url

Example

  1. const url = 'https://example.com/audio.mp3'
  2. 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

Param Type Description
url String audio source url

Example

  1. const url = 'https://example.com/audio.mp3'
  2. player.getAudioDataFromUrl()
  3. .then(arrayBuffer => {
  4. })

player.next() ⇒ Promise

Play next audio source in queue

Kind: instance method of Player
Example

  1. player.next()

player.previous() ⇒ Promise

Play previous audio source in queue

Kind: instance method of Player
Example

  1. player.previous()

player.setRandom(enabled) ⇒ Promise

Enable to disable random playback

Kind: instance method of Player

Param Type Description
enabled Boolean boolean to enable random playback

Example

  1. 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

Param Type Description
enabled Boolean boolean to enable repeat mode

Example

  1. 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

  1. 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

  1. const hasPrevious = player.hasPrevious()

player.setPlaybackRate(playbackRate) ⇒ Promise

Set the plaback rate speed, range 0.75-2

Kind: instance method of Player

Param Type Description
playbackRate Number new playback rate

Example

  1. player.setPlaybackRate(2)

player.getPlaybackRate() ⇒ Number

Get the current plaback rate speed

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

  1. const playbackRate = player.getPlaybackRate()

player.setVolume(volume) ⇒ Promise

Set volume, range 0-1

Kind: instance method of Player

Param Type Description
volume Number volume value

Example

  1. player.setVolume(0.9)

player.getVolume() ⇒ Number

Get current volume value

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

  1. 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

Param Type Description
maxVolume Number max volume, range 0-1

Example

  1. player.setMaxVolume(0.8)

player.getMaxVolume() ⇒ Number

Get max volume value

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

  1. player.getMaxVolume()

player.setMuted(enabled) ⇒ Promise

Set volume level to muted

Kind: instance method of Player

Param Type Description
enabled Boolean boolean to enable mute

Example

  1. 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

  1. player.getCurrentTime()

player.seekTo(seconds) ⇒ Promise

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

  1. const seconds = 45
  2. player.seekTo(seconds)

player.getDuration() ⇒ Number

Get duration of audio source

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

  1. player.getDuration()

player.getCurrentState() ⇒ String

Get current state of player

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

  1. const currentState = player.getCurrentState()
  2. console.log(currentState) // 'playing'

Player.EventTypes ⇒ Object

Return event types

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

  1. const EventTypes = Player.EventTypes
  2. {
  3. LOG: 'log',
  4. ERROR: 'error',
  5. READY: 'ready',
  6. PLAY: 'play',
  7. REPLAY: 'replay',
  8. PAUSE: 'pause',
  9. STOP: 'pause',
  10. NEXT: 'next',
  11. PREVIOUS: 'previous',
  12. RANDOM: 'random',
  13. REPEAT: 'repeat',
  14. PLAYBACK_RATE: 'playbackRate',
  15. VOLUME: 'volume',
  16. MAX_VOLUME: 'maxVolume',
  17. MUTED: 'muted',
  18. ENDED: 'ended',
  19. ENQUEUE: 'enqueue',
  20. DEQUE: 'deque',
  21. EMPTY_QUEUE: 'emptyQueue',
  22. STATE_CHANGE: 'stateChange'
  23. }

Player : object

Class reprensenting a Player

Kind: global namespace
Example

  1. const player = new Player()

new Player()

Create a Player

player.on(eventName, callback)

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

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

Example

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

player.getCurrentUrl() ⇒ String

Get currently playing audio source url

Kind: instance method of Player
Example

  1. const url = player.getCurrentUrl()

player.emptyQueue() ⇒ Promise

Empties queue

Kind: instance method of Player
Example

  1. player.emptyQueue()

player.enqueue(source) ⇒ Promise

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

  1. const url = 'https://example.com/audio.mp3'
  2. player.enqueue(url)

Example

  1. player.enqueue(audioBuffer)

Example

  1. player.enqueue(arrayBuffer)

Example

  1. player.enqueue(blob)

player.deque() ⇒ Promise

Deques an audio source from queue

Kind: instance method of Player
Example

  1. player.deque()

player.play() ⇒ Promise

Plays current audio source in queue

Kind: instance method of Player
Example

  1. player.play()

player.playQueue() ⇒ Promise

Start playing audio sources in queue

Kind: instance method of Player
Example

  1. player.playQueue()

player.stop() ⇒ Promise

Stop playing current audio source

Kind: instance method of Player
Example

  1. player.stop()

player.pause() ⇒ Promise

Pause playing current audio source

Kind: instance method of Player
Example

  1. player.pause()

player.replay() ⇒ Promise

Replay current audio source

Kind: instance method of Player
Example

  1. player.replay()

player.playBlob(blob) ⇒ Promise

Play an audio Blob

Kind: instance method of Player

Param Type Description
blob Blob audio blob

Example

  1. const blob = new Blob([dataView], {
  2. type: 'audio/wav'
  3. })
  4. player.playBlob(blob)

player.playAudioBuffer(audioBuffer) ⇒ Promise

Play an AudioBuffer

Kind: instance method of Player

Param Type Description
audioBuffer AudioBuffer an AudioBuffer

Example

  1. player.playAudioBuffer(audioBuffer)

player.getCurrentAudioBuffer() ⇒ AudioBuffer

Return current audio buffer playing

Kind: instance method of Player
Example

  1. player.getCurrentAudioBuffer()

player.playUrl(url) ⇒ Promise

Play an MP3 url

Kind: instance method of Player

Param Type Description
url String MP3 url

Example

  1. const url = 'https://example.com/audio.mp3'
  2. 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

Param Type Description
url String audio source url

Example

  1. const url = 'https://example.com/audio.mp3'
  2. player.getAudioDataFromUrl()
  3. .then(arrayBuffer => {
  4. })

player.next() ⇒ Promise

Play next audio source in queue

Kind: instance method of Player
Example

  1. player.next()

player.previous() ⇒ Promise

Play previous audio source in queue

Kind: instance method of Player
Example

  1. player.previous()

player.setRandom(enabled) ⇒ Promise

Enable to disable random playback

Kind: instance method of Player

Param Type Description
enabled Boolean boolean to enable random playback

Example

  1. 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

Param Type Description
enabled Boolean boolean to enable repeat mode

Example

  1. 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

  1. 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

  1. const hasPrevious = player.hasPrevious()

player.setPlaybackRate(playbackRate) ⇒ Promise

Set the plaback rate speed, range 0.75-2

Kind: instance method of Player

Param Type Description
playbackRate Number new playback rate

Example

  1. player.setPlaybackRate(2)

player.getPlaybackRate() ⇒ Number

Get the current plaback rate speed

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

  1. const playbackRate = player.getPlaybackRate()

player.setVolume(volume) ⇒ Promise

Set volume, range 0-1

Kind: instance method of Player

Param Type Description
volume Number volume value

Example

  1. player.setVolume(0.9)

player.getVolume() ⇒ Number

Get current volume value

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

  1. 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

Param Type Description
maxVolume Number max volume, range 0-1

Example

  1. player.setMaxVolume(0.8)

player.getMaxVolume() ⇒ Number

Get max volume value

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

  1. player.getMaxVolume()

player.setMuted(enabled) ⇒ Promise

Set volume level to muted

Kind: instance method of Player

Param Type Description
enabled Boolean boolean to enable mute

Example

  1. 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

  1. player.getCurrentTime()

player.seekTo(seconds) ⇒ Promise

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

  1. const seconds = 45
  2. player.seekTo(seconds)

player.getDuration() ⇒ Number

Get duration of audio source

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

  1. player.getDuration()

player.getCurrentState() ⇒ String

Get current state of player

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

  1. const currentState = player.getCurrentState()
  2. console.log(currentState) // 'PLAYING'

Player.EventTypes ⇒ Object

Return event types

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

  1. const EventTypes = Player.EventTypes
  2. {
  3. LOG: 'log',
  4. ERROR: 'error',
  5. READY: 'ready',
  6. PLAY: 'play',
  7. REPLAY: 'replay',
  8. PAUSE: 'pause',
  9. STOP: 'pause',
  10. NEXT: 'next',
  11. PREVIOUS: 'previous',
  12. RANDOM: 'random',
  13. REPEAT: 'repeat',
  14. PLAYBACK_RATE: 'playbackRate',
  15. VOLUME: 'volume',
  16. MAX_VOLUME: 'maxVolume',
  17. MUTED: 'muted',
  18. ENDED: 'ended',
  19. ENQUEUE: 'enqueue',
  20. DEQUE: 'deque',
  21. EMPTY_QUEUE: 'emptyQueue',
  22. STATE_CHANGE: 'stateChange'
  23. }

Usage

Standard audio player

  1. const {Player} = require('audio-director')
  2. const player = new Player()
  3. // add audio source(s) to play queue. Converts input to AudioBuffer.
  4. player.enqueue(dataView|typedArray|arrayBuffer|url)
  5. .then(audioBuffer => {
  6. player.play()
  7. })

Full documentation

YouTube player

  1. const {YoutubePlayer} = require('audio-director')
  2. const player = new YoutubePlayer()
  3. const url = 'https://www.youtube.com/watch?v=_5joTyy3CCo&list=RDQMc4l8l2aQrNo'
  4. player.enqueue(url)
  5. .then(() => player.play())

Full documentation

Example

Live basic example

Source

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

License

MIT