audio-channel-queue 1.7.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +4 -3
- package/dist/errors.js +27 -17
- package/dist/events.js +21 -14
- package/dist/index.d.ts +6 -5
- package/dist/index.js +16 -1
- package/dist/info.js +8 -7
- package/dist/pause.d.ts +83 -0
- package/dist/pause.js +258 -3
- package/dist/types.d.ts +53 -4
- package/dist/types.js +25 -0
- package/dist/utils.js +4 -3
- package/dist/volume.d.ts +28 -3
- package/dist/volume.js +77 -15
- package/package.json +8 -3
- package/src/core.ts +59 -42
- package/src/errors.ts +504 -480
- package/src/events.ts +36 -27
- package/src/index.ts +60 -39
- package/src/info.ts +23 -22
- package/src/pause.ts +328 -23
- package/src/types.ts +57 -3
- package/src/utils.ts +7 -7
- package/src/volume.ts +92 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audio-channel-queue",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Allows you to queue audio files to different playback channels.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,7 +13,11 @@
|
|
|
13
13
|
"prepare": "npm run build",
|
|
14
14
|
"test": "jest",
|
|
15
15
|
"test:watch": "jest --watch",
|
|
16
|
-
"test:coverage": "jest --coverage"
|
|
16
|
+
"test:coverage": "jest --coverage",
|
|
17
|
+
"lint": "npx eslint src/**/*.ts __tests__/**/*.ts",
|
|
18
|
+
"lint:fix": "npx eslint src/**/*.ts __tests__/**/*.ts --fix",
|
|
19
|
+
"format": "npx prettier --write src/**/*.ts __tests__/**/*.ts",
|
|
20
|
+
"format:check": "npx prettier --check src/**/*.ts __tests__/**/*.ts"
|
|
17
21
|
},
|
|
18
22
|
"repository": {
|
|
19
23
|
"type": "git",
|
|
@@ -39,6 +43,7 @@
|
|
|
39
43
|
"jest": "^29.7.0",
|
|
40
44
|
"jest-environment-jsdom": "^29.7.0",
|
|
41
45
|
"ts-jest": "^29.2.5",
|
|
42
|
-
"typescript": "^5.6.2"
|
|
46
|
+
"typescript": "^5.6.2",
|
|
47
|
+
"typescript-eslint": "^8.34.1"
|
|
43
48
|
}
|
|
44
49
|
}
|
package/src/core.ts
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
import { ExtendedAudioQueueChannel, AudioQueueOptions } from './types';
|
|
6
6
|
import { audioChannels } from './info';
|
|
7
7
|
import { extractFileName } from './utils';
|
|
8
|
-
import {
|
|
9
|
-
emitQueueChange,
|
|
10
|
-
emitAudioStart,
|
|
11
|
-
emitAudioComplete,
|
|
12
|
-
setupProgressTracking,
|
|
13
|
-
cleanupProgressTracking
|
|
8
|
+
import {
|
|
9
|
+
emitQueueChange,
|
|
10
|
+
emitAudioStart,
|
|
11
|
+
emitAudioComplete,
|
|
12
|
+
setupProgressTracking,
|
|
13
|
+
cleanupProgressTracking
|
|
14
14
|
} from './events';
|
|
15
15
|
import { applyVolumeDucking, restoreVolumeLevels } from './volume';
|
|
16
16
|
import { setupAudioErrorHandling, handleAudioError } from './errors';
|
|
@@ -30,8 +30,8 @@ import { setupAudioErrorHandling, handleAudioError } from './errors';
|
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
32
|
export const queueAudio = async (
|
|
33
|
-
audioUrl: string,
|
|
34
|
-
channelNumber: number = 0,
|
|
33
|
+
audioUrl: string,
|
|
34
|
+
channelNumber: number = 0,
|
|
35
35
|
options?: AudioQueueOptions
|
|
36
36
|
): Promise<void> => {
|
|
37
37
|
// Ensure the channel exists
|
|
@@ -114,8 +114,8 @@ export const queueAudio = async (
|
|
|
114
114
|
* ```
|
|
115
115
|
*/
|
|
116
116
|
export const queueAudioPriority = async (
|
|
117
|
-
audioUrl: string,
|
|
118
|
-
channelNumber: number = 0,
|
|
117
|
+
audioUrl: string,
|
|
118
|
+
channelNumber: number = 0,
|
|
119
119
|
options?: AudioQueueOptions
|
|
120
120
|
): Promise<void> => {
|
|
121
121
|
const priorityOptions: AudioQueueOptions = { ...options, addToFront: true };
|
|
@@ -152,17 +152,21 @@ export const playAudioQueue = async (channelNumber: number): Promise<void> => {
|
|
|
152
152
|
let hasStarted: boolean = false;
|
|
153
153
|
let metadataLoaded: boolean = false;
|
|
154
154
|
let playStarted: boolean = false;
|
|
155
|
-
|
|
155
|
+
|
|
156
156
|
// Check if we should fire onAudioStart (both conditions met)
|
|
157
157
|
const tryFireAudioStart = (): void => {
|
|
158
158
|
if (!hasStarted && metadataLoaded && playStarted) {
|
|
159
159
|
hasStarted = true;
|
|
160
|
-
emitAudioStart(
|
|
160
|
+
emitAudioStart(
|
|
161
161
|
channelNumber,
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
{
|
|
163
|
+
channelNumber,
|
|
164
|
+
duration: currentAudio.duration * 1000,
|
|
165
|
+
fileName: extractFileName(currentAudio.src),
|
|
166
|
+
src: currentAudio.src
|
|
167
|
+
},
|
|
168
|
+
audioChannels
|
|
169
|
+
);
|
|
166
170
|
}
|
|
167
171
|
};
|
|
168
172
|
|
|
@@ -180,12 +184,16 @@ export const playAudioQueue = async (channelNumber: number): Promise<void> => {
|
|
|
180
184
|
|
|
181
185
|
// Event handler for when audio ends
|
|
182
186
|
const handleEnded = async (): Promise<void> => {
|
|
183
|
-
emitAudioComplete(
|
|
187
|
+
emitAudioComplete(
|
|
184
188
|
channelNumber,
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
+
{
|
|
190
|
+
channelNumber,
|
|
191
|
+
fileName: extractFileName(currentAudio.src),
|
|
192
|
+
remainingInQueue: channel.queue.length - 1,
|
|
193
|
+
src: currentAudio.src
|
|
194
|
+
},
|
|
195
|
+
audioChannels
|
|
196
|
+
);
|
|
189
197
|
|
|
190
198
|
// Restore volume levels when priority channel stops
|
|
191
199
|
await restoreVolumeLevels(channelNumber);
|
|
@@ -194,9 +202,9 @@ export const playAudioQueue = async (channelNumber: number): Promise<void> => {
|
|
|
194
202
|
currentAudio.removeEventListener('loadedmetadata', handleLoadedMetadata);
|
|
195
203
|
currentAudio.removeEventListener('play', handlePlay);
|
|
196
204
|
currentAudio.removeEventListener('ended', handleEnded);
|
|
197
|
-
|
|
205
|
+
|
|
198
206
|
cleanupProgressTracking(currentAudio, channelNumber, audioChannels);
|
|
199
|
-
|
|
207
|
+
|
|
200
208
|
// Handle looping vs non-looping audio
|
|
201
209
|
if (currentAudio.loop) {
|
|
202
210
|
// For looping audio, reset current time and continue playing
|
|
@@ -210,10 +218,10 @@ export const playAudioQueue = async (channelNumber: number): Promise<void> => {
|
|
|
210
218
|
} else {
|
|
211
219
|
// For non-looping audio, remove from queue and play next
|
|
212
220
|
channel.queue.shift();
|
|
213
|
-
|
|
221
|
+
|
|
214
222
|
// Emit queue change after completion
|
|
215
223
|
setTimeout(() => emitQueueChange(channelNumber, audioChannels), 10);
|
|
216
|
-
|
|
224
|
+
|
|
217
225
|
await playAudioQueue(channelNumber);
|
|
218
226
|
resolve();
|
|
219
227
|
}
|
|
@@ -225,7 +233,7 @@ export const playAudioQueue = async (channelNumber: number): Promise<void> => {
|
|
|
225
233
|
currentAudio.addEventListener('ended', handleEnded);
|
|
226
234
|
|
|
227
235
|
// Check if metadata is already loaded (in case it loads before we add the listener)
|
|
228
|
-
if (currentAudio.readyState >= 1) {
|
|
236
|
+
if (currentAudio.readyState >= 1) {
|
|
229
237
|
metadataLoaded = true;
|
|
230
238
|
}
|
|
231
239
|
|
|
@@ -250,13 +258,17 @@ export const stopCurrentAudioInChannel = async (channelNumber: number = 0): Prom
|
|
|
250
258
|
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
251
259
|
if (channel && channel.queue.length > 0) {
|
|
252
260
|
const currentAudio: HTMLAudioElement = channel.queue[0];
|
|
253
|
-
|
|
254
|
-
emitAudioComplete(
|
|
261
|
+
|
|
262
|
+
emitAudioComplete(
|
|
255
263
|
channelNumber,
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
264
|
+
{
|
|
265
|
+
channelNumber,
|
|
266
|
+
fileName: extractFileName(currentAudio.src),
|
|
267
|
+
remainingInQueue: channel.queue.length - 1,
|
|
268
|
+
src: currentAudio.src
|
|
269
|
+
},
|
|
270
|
+
audioChannels
|
|
271
|
+
);
|
|
260
272
|
|
|
261
273
|
// Restore volume levels when stopping
|
|
262
274
|
await restoreVolumeLevels(channelNumber);
|
|
@@ -267,8 +279,9 @@ export const stopCurrentAudioInChannel = async (channelNumber: number = 0): Prom
|
|
|
267
279
|
channel.isPaused = false; // Reset pause state
|
|
268
280
|
|
|
269
281
|
emitQueueChange(channelNumber, audioChannels);
|
|
270
|
-
|
|
282
|
+
|
|
271
283
|
// Start next audio without waiting for it to complete
|
|
284
|
+
// eslint-disable-next-line no-console
|
|
272
285
|
playAudioQueue(channelNumber).catch(console.error);
|
|
273
286
|
}
|
|
274
287
|
};
|
|
@@ -287,13 +300,17 @@ export const stopAllAudioInChannel = async (channelNumber: number = 0): Promise<
|
|
|
287
300
|
if (channel) {
|
|
288
301
|
if (channel.queue.length > 0) {
|
|
289
302
|
const currentAudio: HTMLAudioElement = channel.queue[0];
|
|
290
|
-
|
|
291
|
-
emitAudioComplete(
|
|
303
|
+
|
|
304
|
+
emitAudioComplete(
|
|
292
305
|
channelNumber,
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
306
|
+
{
|
|
307
|
+
channelNumber,
|
|
308
|
+
fileName: extractFileName(currentAudio.src),
|
|
309
|
+
remainingInQueue: 0, // Will be 0 since we're clearing the queue
|
|
310
|
+
src: currentAudio.src
|
|
311
|
+
},
|
|
312
|
+
audioChannels
|
|
313
|
+
);
|
|
297
314
|
|
|
298
315
|
// Restore volume levels when stopping
|
|
299
316
|
await restoreVolumeLevels(channelNumber);
|
|
@@ -302,10 +319,10 @@ export const stopAllAudioInChannel = async (channelNumber: number = 0): Promise<
|
|
|
302
319
|
cleanupProgressTracking(currentAudio, channelNumber, audioChannels);
|
|
303
320
|
}
|
|
304
321
|
// Clean up all progress tracking for this channel
|
|
305
|
-
channel.queue.forEach(audio => cleanupProgressTracking(audio, channelNumber, audioChannels));
|
|
322
|
+
channel.queue.forEach((audio) => cleanupProgressTracking(audio, channelNumber, audioChannels));
|
|
306
323
|
channel.queue = [];
|
|
307
324
|
channel.isPaused = false; // Reset pause state
|
|
308
|
-
|
|
325
|
+
|
|
309
326
|
emitQueueChange(channelNumber, audioChannels);
|
|
310
327
|
}
|
|
311
328
|
};
|
|
@@ -323,4 +340,4 @@ export const stopAllAudio = async (): Promise<void> => {
|
|
|
323
340
|
stopPromises.push(stopAllAudioInChannel(index));
|
|
324
341
|
});
|
|
325
342
|
await Promise.all(stopPromises);
|
|
326
|
-
};
|
|
343
|
+
};
|