Last active
November 20, 2019 21:17
-
-
Save navio/5f8c963a59eb811458f464ac7a152613 to your computer and use it in GitHub Desktop.
Audio Engine 1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IAudioParams { | |
image?: URL; | |
author?: String; | |
title: String; | |
src: URL; | |
} | |
class AudioEngine { | |
private engine; | |
private queue = []; | |
private current; | |
private autoPlay; | |
constructor(init: IAudioParams[] = [], config: { audioObject?: AudioContext, autoplay?: boolean } = {}){ | |
this.queue = [...init]; | |
this.engine = config.audioObject || new Audio(); | |
this.autoPlay = config.autoplay || true; | |
this.engine.addEventListener('ended',() => { | |
if( this.autoPlay ){ | |
this.next(); | |
} | |
}); | |
} | |
get list(){ | |
return this.queue; | |
} | |
get audio(){ | |
return this.engine; | |
} | |
set autoplay(status: Boolean){ | |
this.autoPlay = status; | |
} | |
public play() { | |
if (this.current) { | |
this.engine.play(); | |
return true; | |
} else { | |
return this.next(); | |
} | |
} | |
public next(){ | |
if(!this.queue.length){ | |
return false; | |
} | |
this.current = this.queue.shift(); | |
if (this.current) { | |
this.engine.src = this.current.src; | |
} | |
this.engine.play(); | |
return true; | |
} | |
public pause() { | |
this.engine.pause(); | |
return true; | |
} | |
public addLast(audio:IAudioParams) { | |
return this.queue.push(audio); | |
} | |
public addNext(audio:IAudioParams) { | |
return this.queue.unshift(audio); | |
} | |
} | |
/* | |
const player = new AudioEngine([{ title: "Audio", src: new URL(mp3URL)},{ title: "Audio1", src: new URL(mp3URL1)}]); | |
player.next(); | |
player.pause(); | |
player.next(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment