-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioQueue.ts
More file actions
147 lines (126 loc) · 3.46 KB
/
AudioQueue.ts
File metadata and controls
147 lines (126 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
interface IAudioParams {
[key: string]: any;
src?: String;
srcObject?: MediaStream | MediaSource | Blob;
}
class AudioQueueClass {
private engine;
private queue = [];
private current: IAudioParams;
public autoplay;
public SETTERS = ["src","srcObject"];
constructor(
init: IAudioParams[] = [],
config: { audioObject?: HTMLElement; autoplay?: Boolean } = {}
) {
this.queue = [...init];
this.engine = config.audioObject || new Audio();
this.autoplay = config.autoplay || true;
this.current = null;
this.engine.addEventListener("ended", () => {
if (this.autoplay) {
this.next();
}
});
}
get list(): IAudioParams[] {
return this.queue;
}
get selected(): IAudioParams {
return this.current;
}
set src(value: String){
if (this.current){
this.current.currentTime = this.engine.currentTime;
this.addNext(this.current);
}
this.current = { src: value }
this.engine.src = value;
}
set srcObject( value: MediaStream | MediaSource | Blob ){
if (this.current){
this.current.currentTime = this.engine.currentTime;
this.addNext(this.current);
}
this.current = { srcObject: value }
this.engine.srcObject = value;
}
public play() {
if (this.current) {
return this.engine.play();
} else {
return this.next();
}
}
public next() {
if (!this.queue.length) {
const event = new Event("EmptyQueue");
this.engine.dispatchEvent(event);
this.current = null;
this.engine.pause();
this.engine.src = null;
this.engine.srcObject = null;
return;
}
this.current = this.queue.shift();
if (this.current) {
const { src, srcObject, currentTime = 0 } = this.current;
if(!src && !srcObject) {
const event = new Event("NoSource");
this.engine.dispatchEvent(event);
return this.next();
}
if (src) this.engine.src = src;
if (srcObject) this.engine.srcObject = srcObject
this.engine.currentTime = currentTime;
this.engine.play();
}
}
public addLast(audio: IAudioParams) {
return this.queue.push(audio);
}
public addNext(audio: IAudioParams) {
return this.queue.unshift(audio);
}
public restart() {
return this.engine.currentTime = 0;
}
}
export default class AudioQueue {
private QueueClass;
private AudioObject;
constructor(
init: IAudioParams[] = [],
config: { audioObject?: HTMLElement; autoplay?: boolean } = {}
) {
const { audioObject = new Audio() } = config;
this.QueueClass = new AudioQueueClass(init, { audioObject, ...config });
this.AudioObject = audioObject;
return new Proxy(this.QueueClass, {
get: (target, property) => {
if (property in target) {
return target[property];
}
if (property in this.AudioObject) {
// Audio Object requires context variables that are lost in the Proxy.
const prop = this.AudioObject[property];
return ( typeof prop === 'function') ?
prop.bind(this.AudioObject):
prop;
}
},
set: (target, property, value) => {
if(target.SETTERS.includes(property)){
return target[property] = value;
}
if (property in this.AudioObject) {
this.AudioObject[property] = value;
}
if (property in target) {
target[property] = value;
}
return true;
}
});
}
}