-
Notifications
You must be signed in to change notification settings - Fork 0
State Management
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.
Provide your callbacks as an object to constructor's argument:
const audio = new dAudio({
onload: () => { /* your script */ },
onplay: () => { /* your script */ }
});
Provide your callbacks after construction:
const audio = new dAudio();
audio.onload = () => { /* your script */ };
audio.onplay = () => { /* your script */ }
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
state comes up when dAudio
starts to load an audio file.
audio.onload = () => { /* your script */ };
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
state comes up after load
state when dAudio
detects the source is a Blob
.
audio.onoffline = () => { /* your script */ };
decode
state comes up after online
or offline
states when dAudio
starts to decode the audio file.
audio.ondecode = () => { /* your script */ };
remaster
state comes up after decode
state when dAudio
starts to scan and analyze the audio file.
audio.onremaster = () => { /* your script */ };
ready
state comes up after remaster
state when dAudio
is ready to play.
audio.onready = () => { /* your script */ };
autoplay
state comes up after ready
state when dAudio
checks for autoplay setting and autoplay is on.
audio.onautoplay = () => { /* your script */ };
play
state comes up after ready
or autoplay
states when dAudio
starts playing.
audio.onplay = () => { /* your script */ };
pause
state comes up after play
state when dAudio
pauses the playback.
audio.onpause = () => { /* your script */ };
stop
state comes up after play
or pause
states when dAudio
stops the playback.
audio.onstop = () => { /* your script */ };
end
state comes up after stop
state when dAudio
has reached to the end of audio file.
audio.onstop = () => { /* your script */ };
repeat
state comes up after end
state when dAudio
checks for repeat setting and repeat is on.
audio.onrepeat = () => { /* your script */ };
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}`)
}
- OK
- Incompatible Browser
- Unsupported Audio Codec
- Invalid Audio File Type
- Audio File Not Found
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}`)
}
};