@bendyline/squisq-react 2.11.23 → 2.11.25

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.
@@ -24,6 +24,13 @@ interface AudioState {
24
24
  isEnded: boolean;
25
25
  /** Whether audio is loaded and ready to play */
26
26
  isReady: boolean;
27
+ /**
28
+ * Whether playback was requested but no sound has started yet: the segment
29
+ * is still downloading or decoding, or playback stalled. Controls show a
30
+ * loading state on the Play/Pause button. Optional for controllers that
31
+ * cannot tell (an absent value means "not buffering").
32
+ */
33
+ isBuffering?: boolean;
27
34
  /** Whether the audio backend is available/connected */
28
35
  isAvailable: boolean;
29
36
  /** Message to show when not available */
@@ -58,6 +58,12 @@ function findSegmentAtTime(time, segments, segmentStarts) {
58
58
  // src/hooks/useAudioSync.ts
59
59
  var SEEK_COMPLETION_TIMEOUT_MS = 15e3;
60
60
  var UNLOADABLE_MESSAGE = "Audio could not be loaded. Check that the media file is available.";
61
+ var PLAYBACK_FAILED_MESSAGE = "Audio playback failed. Check that the media file is supported and available.";
62
+ var NEXT_SEGMENT_WARM_LEAD_SECONDS = 30;
63
+ function isBenignPlayRejection(error) {
64
+ if (!(error instanceof Error)) return false;
65
+ return error.name === "NotAllowedError" || error.name === "AbortError";
66
+ }
61
67
  function resolveAudioUrl(src, basePath) {
62
68
  if (!src || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(src)) return src;
63
69
  if (!basePath) return src;
@@ -80,6 +86,7 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
80
86
  const [totalDuration, setTotalDuration] = useState(0);
81
87
  const [isAvailable, setIsAvailable] = useState(true);
82
88
  const [unavailableMessage, setUnavailableMessage] = useState();
89
+ const [isAudioFlowing, setIsAudioFlowing] = useState(false);
83
90
  const segmentStarts = useRef([]);
84
91
  const pendingSeekTime = useRef(null);
85
92
  const pendingSeekCompletion = useRef(null);
@@ -91,6 +98,8 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
91
98
  const activeSegmentSrc = useRef(void 0);
92
99
  activeSegmentSrc.current = audioTrack?.segments[currentSegment]?.src;
93
100
  const boundSource = useRef(null);
101
+ const loadingSegment = useRef(null);
102
+ const warmedNextSegment = useRef(null);
94
103
  const currentSegmentRef = useRef(0);
95
104
  currentSegmentRef.current = currentSegment;
96
105
  const fallbackMode = useRef(false);
@@ -102,6 +111,8 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
102
111
  shouldPlayAfterLoad.current = false;
103
112
  fallbackMode.current = false;
104
113
  boundSource.current = null;
114
+ loadingSegment.current = null;
115
+ warmedNextSegment.current = null;
105
116
  setCurrentTime(0);
106
117
  setCurrentSegment(0);
107
118
  setIsPlaying(false);
@@ -109,6 +120,7 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
109
120
  setIsAudioReady(false);
110
121
  setIsAvailable(true);
111
122
  setUnavailableMessage(void 0);
123
+ setIsAudioFlowing(false);
112
124
  if (!enabled || !audioTrack?.segments) {
113
125
  segmentStarts.current = [];
114
126
  setTotalDuration(0);
@@ -198,19 +210,32 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
198
210
  if (!enabled || mode === "synthetic") return;
199
211
  const audio = audioRef.current;
200
212
  if (!audio) return;
213
+ warmedNextSegment.current = null;
201
214
  const handleTimeUpdate = () => {
202
215
  if (fallbackMode.current) return;
203
216
  const segmentStart = segmentStarts.current[currentSegment] || 0;
204
217
  const overallTime = segmentStart + audio.currentTime;
205
218
  setCurrentTime(overallTime);
219
+ const active = audioTrack?.segments[currentSegment];
220
+ const next = audioTrack?.segments[currentSegment + 1];
221
+ if (active && next && warmedNextSegment.current !== next.src && active.duration - audio.currentTime <= NEXT_SEGMENT_WARM_LEAD_SECONDS) {
222
+ warmedNextSegment.current = next.src;
223
+ void preloadAudio(next.src);
224
+ }
206
225
  };
207
226
  const handlePlay = () => {
208
227
  setIsPlaying(true);
209
228
  };
210
- const handlePause = () => setIsPlaying(false);
229
+ const handlePause = () => {
230
+ setIsPlaying(false);
231
+ setIsAudioFlowing(false);
232
+ };
233
+ const handlePlaying = () => setIsAudioFlowing(true);
234
+ const handleWaiting = () => setIsAudioFlowing(false);
211
235
  const handleError = () => {
212
236
  setIsAudioReady(true);
213
237
  setIsPlaying(false);
238
+ setIsAudioFlowing(false);
214
239
  setIsAvailable(false);
215
240
  setUnavailableMessage(UNLOADABLE_MESSAGE);
216
241
  shouldPlayAfterLoad.current = false;
@@ -232,17 +257,25 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
232
257
  };
233
258
  audio.addEventListener("timeupdate", handleTimeUpdate);
234
259
  audio.addEventListener("play", handlePlay);
260
+ audio.addEventListener("playing", handlePlaying);
261
+ audio.addEventListener("waiting", handleWaiting);
235
262
  audio.addEventListener("pause", handlePause);
236
263
  audio.addEventListener("ended", handleEnded);
237
264
  audio.addEventListener("error", handleError);
238
265
  return () => {
239
266
  audio.removeEventListener("timeupdate", handleTimeUpdate);
240
267
  audio.removeEventListener("play", handlePlay);
268
+ audio.removeEventListener("playing", handlePlaying);
269
+ audio.removeEventListener("waiting", handleWaiting);
241
270
  audio.removeEventListener("pause", handlePause);
242
271
  audio.removeEventListener("ended", handleEnded);
243
272
  audio.removeEventListener("error", handleError);
244
273
  };
245
- }, [audioRef, currentSegment, audioTrack, enabled, mode]);
274
+ }, [audioRef, currentSegment, audioTrack, enabled, mode, preloadAudio]);
275
+ const isBoundTo = useCallback((audio, segmentKey) => {
276
+ const bound = boundSource.current;
277
+ return !!audio.src && bound?.key === segmentKey && bound.url === audio.src;
278
+ }, []);
246
279
  useEffect(() => {
247
280
  if (!enabled || mode === "synthetic") return;
248
281
  const audio = audioRef.current;
@@ -260,27 +293,32 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
260
293
  pendingSeekCompletion.current = null;
261
294
  }
262
295
  if (shouldPlayAfterLoad.current) {
263
- audio.play().catch((error) => {
264
- setIsPlaying(false);
265
- if (!(error instanceof Error && error.name === "NotAllowedError")) {
266
- setIsAvailable(false);
267
- setUnavailableMessage(
268
- "Audio playback failed. Check that the media file is supported and available."
269
- );
296
+ audio.play().then(
297
+ () => {
298
+ setIsAvailable(true);
299
+ setUnavailableMessage(void 0);
300
+ },
301
+ (error) => {
302
+ if (error instanceof Error && error.name === "AbortError") return;
303
+ setIsPlaying(false);
304
+ if (!isBenignPlayRejection(error)) {
305
+ setIsAvailable(false);
306
+ setUnavailableMessage(PLAYBACK_FAILED_MESSAGE);
307
+ }
270
308
  }
271
- });
309
+ );
272
310
  shouldPlayAfterLoad.current = false;
273
311
  }
274
312
  };
275
- const currentSrc = audio.src;
276
- const bound = boundSource.current;
277
- const isSameSource = !!currentSrc && bound?.key === segment.src && bound.url === currentSrc;
313
+ const isSameSource = isBoundTo(audio, segment.src);
278
314
  let cancelled = false;
279
315
  let handleCanPlay = null;
280
316
  if (!isSameSource) {
281
317
  const loadAndPlay = async () => {
318
+ loadingSegment.current = segment.src;
282
319
  const blobUrl = await preloadAudio(segment.src);
283
320
  if (cancelled) return;
321
+ loadingSegment.current = null;
284
322
  if (!blobUrl) {
285
323
  audio.removeAttribute("src");
286
324
  audio.load();
@@ -308,6 +346,8 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
308
346
  audio.src = blobUrl;
309
347
  boundSource.current = { key: segment.src, url: audio.src };
310
348
  audio.load();
349
+ setIsAudioFlowing(false);
350
+ if (!shouldPlayAfterLoad.current) setIsPlaying(false);
311
351
  await Promise.resolve();
312
352
  if (audio.readyState >= 3) {
313
353
  if (handleCanPlay) audio.removeEventListener("canplay", handleCanPlay);
@@ -317,13 +357,14 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
317
357
  };
318
358
  void loadAndPlay();
319
359
  } else {
360
+ if (loadingSegment.current === segment.src) loadingSegment.current = null;
320
361
  applyPendingSeek();
321
362
  }
322
363
  return () => {
323
364
  cancelled = true;
324
365
  if (handleCanPlay) audio.removeEventListener("canplay", handleCanPlay);
325
366
  };
326
- }, [audioRef, currentSegment, audioTrack, preloadAudio, enabled, mode]);
367
+ }, [audioRef, currentSegment, audioTrack, preloadAudio, enabled, mode, isBoundTo]);
327
368
  const seekTo = useCallback(
328
369
  async (time) => {
329
370
  const audio = audioRef.current;
@@ -345,6 +386,9 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
345
386
  if (segmentIndex !== currentSegmentRef.current) {
346
387
  pendingSeekTime.current = clampedTime;
347
388
  shouldPlayAfterLoad.current = wasPlaying;
389
+ if (mode === "media" && enabled) {
390
+ loadingSegment.current = audioTrack.segments[segmentIndex].src;
391
+ }
348
392
  const completion = new Promise((resolve) => {
349
393
  pendingSeekCompletion.current?.();
350
394
  let settled = false;
@@ -372,7 +416,7 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
372
416
  setCurrentTime(clampedTime);
373
417
  }
374
418
  },
375
- [audioRef, audioTrack, isPlaying, mode, totalDuration]
419
+ [audioRef, audioTrack, enabled, isPlaying, mode, totalDuration]
376
420
  );
377
421
  const play = useCallback(async () => {
378
422
  if (mode === "synthetic") {
@@ -385,6 +429,16 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
385
429
  if (isEnded) {
386
430
  await seekTo(0);
387
431
  }
432
+ const segment = audioTrack?.segments[currentSegmentRef.current];
433
+ if (segment && loadingSegment.current === segment.src && !isBoundTo(audio, segment.src)) {
434
+ shouldPlayAfterLoad.current = true;
435
+ setIsPlaying(true);
436
+ if (!audio.getAttribute("src")) {
437
+ audio.play().catch(() => {
438
+ });
439
+ }
440
+ return;
441
+ }
388
442
  try {
389
443
  await audio.play();
390
444
  fallbackMode.current = false;
@@ -392,18 +446,20 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
392
446
  setUnavailableMessage(void 0);
393
447
  } catch (error) {
394
448
  fallbackMode.current = false;
449
+ if (error instanceof Error && error.name === "AbortError") {
450
+ if (!shouldPlayAfterLoad.current) setIsPlaying(false);
451
+ return;
452
+ }
395
453
  setIsPlaying(false);
396
- const name = error instanceof Error ? error.name : "";
397
- if (name !== "NotAllowedError") {
454
+ if (!isBenignPlayRejection(error)) {
398
455
  setIsAvailable(false);
399
- setUnavailableMessage(
400
- "Audio playback failed. Check that the media file is supported and available."
401
- );
456
+ setUnavailableMessage(PLAYBACK_FAILED_MESSAGE);
402
457
  }
403
458
  }
404
459
  }
405
- }, [audioRef, isEnded, mode, seekTo]);
460
+ }, [audioRef, audioTrack, isEnded, isBoundTo, mode, seekTo]);
406
461
  const pause = useCallback(() => {
462
+ shouldPlayAfterLoad.current = false;
407
463
  const audio = audioRef.current;
408
464
  if (audio) {
409
465
  audio.pause();
@@ -464,6 +520,7 @@ function useAudioSync(audioRef, track, basePath = "", enabled = true, mode = "me
464
520
  totalDuration,
465
521
  isEnded,
466
522
  isReady: isAudioReady,
523
+ isBuffering: mode === "media" && isPlaying && !isAudioFlowing,
467
524
  isAvailable,
468
525
  unavailableMessage,
469
526
  // Actions