audio-channel-queue 1.8.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/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(channelNumber, {
160
+ emitAudioStart(
161
161
  channelNumber,
162
- duration: currentAudio.duration * 1000, // Now guaranteed to have valid duration
163
- fileName: extractFileName(currentAudio.src),
164
- src: currentAudio.src
165
- }, audioChannels);
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(channelNumber, {
187
+ emitAudioComplete(
184
188
  channelNumber,
185
- fileName: extractFileName(currentAudio.src),
186
- remainingInQueue: channel.queue.length - 1,
187
- src: currentAudio.src
188
- }, audioChannels);
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) { // HAVE_METADATA or higher
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(channelNumber, {
261
+
262
+ emitAudioComplete(
255
263
  channelNumber,
256
- fileName: extractFileName(currentAudio.src),
257
- remainingInQueue: channel.queue.length - 1,
258
- src: currentAudio.src
259
- }, audioChannels);
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(channelNumber, {
303
+
304
+ emitAudioComplete(
292
305
  channelNumber,
293
- fileName: extractFileName(currentAudio.src),
294
- remainingInQueue: 0, // Will be 0 since we're clearing the queue
295
- src: currentAudio.src
296
- }, audioChannels);
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
+ };