Skip to content

State Management

Farshad Javan edited this page Dec 17, 2019 · 9 revisions

Providing Callbacks

dAudio object reports every step of it's functionality by changing the state property and calling the callback functions for each state if provided by user. You have two ways to provide your callbacks.

Method 1

Provide your callbacks as an object to constructor's argument:

const audio = new dAudio({
  onload: () => { /* your script */ },
  onplay: () => { /* your script */ }
});

Method 2

Provide your callbacks after construction:

const audio = new dAudio();
audio.onload = () => { /* your script */ };
audio.onplay = () => { /* your script */ }

States

init

This is the first state during the construction and happens only one time. If you need this state for your application, you have to provide the callback in constructor like below:

const audio = new dAudio({
  oninit: () => { /* your script */ }
});

load

load state comes up when dAudio starts to load an audio file.

audio.onload = () => { /* your script */ };

online

online state comes up after load state when dAudio detects the source is a URL and needs to be downloaded.

audio.ononline = () => { /* your script */ };

offline

offline state comes up after load state when dAudio detects the source is a Blob.

audio.onoffline = () => { /* your script */ };

decode

decode state comes up after online or offline states when dAudio starts to decode the audio file.

audio.ondecode = () => { /* your script */ };

remaster

remaster state comes up after decode state when dAudio starts to scan and analyze the audio file.

audio.onremaster = () => { /* your script */ };

ready

ready state comes up after remaster state when dAudio is ready to play.

audio.onready = () => { /* your script */ };

autoplay

autoplay state comes up after ready state when dAudio checks for autoplay setting and autoplay is on.

audio.onautoplay = () => { /* your script */ };

play

play state comes up after ready or autoplay states when dAudio starts playing.

audio.onplay = () => { /* your script */ };

pause

pause state comes up after play state when dAudio pauses the playback.

audio.onpause = () => { /* your script */ };

stop

stop state comes up after play or pause states when dAudio stops the playback.

audio.onstop = () => { /* your script */ };

end

end state comes up after stop state when dAudio has reached to the end of audio file.

audio.onstop = () => { /* your script */ };

repeat

repeat state comes up after end state when dAudio checks for repeat setting and repeat is on.

audio.onrepeat = () => { /* your script */ };

error

error state may comes up every where when something wrong happens. error state sends back code and message properties to your callback:

audio.onerror = error => {
  console.log(`Err: ${error.code}`, `Msg: ${error.message}`)
}

error codes and messages

  1. OK
  2. Incompatible Browser
  3. Unsupported Audio Codec
  4. Invalid Audio File Type
  5. Audio File Not Found

Grab All States

dAudio triggers a callback on every state change, named onstate. You can grab all the states on a single callback. If you need init state, you have to pass your onstate callback function to the constructor argument:

const audio = new dAudio({
  onstate: state => {
    if (state.name == 'init') { /* your script */ }
    if (state.name == 'load') { /* your script */ }
    if (state.name == 'error') {
      console.log(`Err: ${state.code}`, `Msg: ${state.message}`)
    }
  }
});

// OR //

const audio = new dAudio();
audio.onstate = state => {
    if (state.name == 'load') { /* your script */ }
    if (state.name == 'play') { /* your script */ }
    if (state.name == 'error') {
      console.log(`Err: ${state.code}`, `Msg: ${state.message}`)
    }
};