bloom-player 2.20.1-alpha.5 → 2.20.1-alpha.6

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A library for displaying Bloom books in iframes or WebViews",
4
4
  "author": "SIL Global",
5
5
  "license": "MIT",
6
- "version": "2.20.1-alpha.5",
6
+ "version": "2.20.1-alpha.6",
7
7
  "packageManager": "pnpm@11.1.2",
8
8
  "private": false,
9
9
  "// sideeffects might need to be ['*.css'] to avoid 'shaking' our CSS, if we ever get tree shaking working": "",
@@ -14,6 +14,7 @@
14
14
 
15
15
  import { ActivityManager } from "../activities/activityManager";
16
16
  import {
17
+ cancelVideoFirstFramePriming,
17
18
  kAudioSentence,
18
19
  playAllAudio,
19
20
  playAllVideo,
@@ -186,9 +187,7 @@ export function prepareActivity(
186
187
  // Ensure the first frame is visible. The transparent poster (set globally by
187
188
  // bloom-player at book load) hides the video until playback begins. Non-draggable
188
189
  // videos get a play+pause first-frame trick in video.ts HandlePageVisible, but
189
- // draggable videos are skipped there. If the video source hasn't loaded yet
190
- // (e.g. cold cache after a build), play() fails silently, leaving the video blank.
191
- // We use a loadeddata listener so the trick runs whenever the data is available.
190
+ // draggable videos are skipped there, so they depend entirely on this call.
192
191
  showVideoFirstFrameWhenReady(video);
193
192
  }
194
193
  });
@@ -297,6 +296,9 @@ const prepareOrderSentenceActivity = (page: HTMLElement) => {
297
296
 
298
297
  const playVideo = (e: MouseEvent) => {
299
298
  const video = e.currentTarget as HTMLVideoElement;
299
+ // The user asked for real playback; a pending first-frame priming attempt
300
+ // must not mute or pause it.
301
+ cancelVideoFirstFramePriming(video);
300
302
  video.play();
301
303
  };
302
304
 
@@ -982,6 +984,19 @@ function showCorrectOrWrongItems(page: HTMLElement, correct: boolean) {
982
984
  classSetter(page, "drag-activity-wrong", !correct);
983
985
 
984
986
  const playOtherStuff = () => {
987
+ // This runs when the correct/wrong sound finishes, which may be after
988
+ // the user has already clicked "Show Correct" or "Try Again". Those
989
+ // take the page out of the state this feedback belongs to, and
990
+ // "Show Correct" starts playing the solution videos, which the
991
+ // playAllVideo call below would silently cancel (BL-16146). If the
992
+ // page is no longer showing the state we were queued for, do nothing.
993
+ if (
994
+ !page.parentElement?.classList.contains(
995
+ correct ? "drag-activity-correct" : "drag-activity-wrong",
996
+ )
997
+ ) {
998
+ return;
999
+ }
985
1000
  const elementsMadeVisible = Array.from(
986
1001
  page.getElementsByClassName(
987
1002
  correct ? "drag-item-correct" : "drag-item-wrong",
@@ -1401,94 +1401,134 @@ export function isTransientVideoPlayFailure(reason: any): boolean {
1401
1401
  return message.includes("interrupted by a call to pause()");
1402
1402
  }
1403
1403
 
1404
- // Attempt to show a video's first frame by briefly starting playback and then pausing.
1405
- // The callback allows callers to decide at the moment playback starts whether pausing
1406
- // is still desired (for example, page-level logic may stop pausing after initial load).
1404
+ // Cancel functions for in-progress first-frame priming attempts (see
1405
+ // showVideoFirstFrameWhenReady below), keyed by the video being primed.
1406
+ const firstFramePrimingCancels = new WeakMap<HTMLVideoElement, () => void>();
1407
+
1408
+ // Anything about to start real playback of a video should call this first:
1409
+ // if a first-frame priming attempt is in progress on the video, it is
1410
+ // cancelled (restoring the video's muted state and removing the transparent
1411
+ // poster) so the primer can't mute or pause the real playback.
1412
+ export function cancelVideoFirstFramePriming(video: HTMLVideoElement) {
1413
+ firstFramePrimingCancels.get(video)?.();
1414
+ }
1415
+
1416
+ // Attempt to show a video's first frame by briefly starting muted playback and
1417
+ // then pausing. We mute while priming because gestureless unmuted play() is
1418
+ // rejected by autoplay policies (iOS especially, where a past tap elsewhere on
1419
+ // the page grants nothing). Calling play() is also what forces browsers that
1420
+ // don't preload video data (again, iOS Safari) to fetch and decode the first
1421
+ // frame, so we must not wait for loadeddata first: on iOS it typically never
1422
+ // fires until something plays. (BL-16146)
1423
+ // The callback allows callers to decide at the moment playback starts whether
1424
+ // pausing is still desired (for example, page-level logic may stop pausing
1425
+ // after initial load).
1407
1426
  export function showVideoFirstFrameWhenReady(
1408
1427
  video: HTMLVideoElement,
1409
1428
  shouldPauseAfterPlaying: () => boolean = () => true,
1410
1429
  onAutoplayBlocked: () => void = () => showVideoAutoplayBlockedHint(video),
1411
1430
  ) {
1412
- let canceled = false;
1413
- const attempt = () => {
1414
- if (canceled) {
1431
+ // If something else has already started playback, don't interfere.
1432
+ if (!video.paused) {
1433
+ return;
1434
+ }
1435
+ // If an earlier priming attempt on this video is still armed (e.g. the
1436
+ // page was left and re-entered within the give-up window), cancel it so
1437
+ // there is only ever one live attempt per video — otherwise the old
1438
+ // attempt's listeners/timer linger, and this attempt would wrongly record
1439
+ // the old attempt's muting as the video's real muted state.
1440
+ cancelVideoFirstFramePriming(video);
1441
+ const wasMuted = video.muted;
1442
+ let giveUpTimeout: number | undefined;
1443
+ let done = false;
1444
+ // Idempotent cleanup: whatever way the priming attempt ends, restore the
1445
+ // video's real muted state and stop listening.
1446
+ const finish = () => {
1447
+ if (done) {
1415
1448
  return;
1416
1449
  }
1417
- // If something else has already started playback, don't attach our
1418
- // pause-on-playing behavior.
1419
- if (!video.paused) {
1450
+ done = true;
1451
+ // Deregister only our own cancel function, in case a newer priming
1452
+ // attempt has already replaced it.
1453
+ if (firstFramePrimingCancels.get(video) === cancel) {
1454
+ firstFramePrimingCancels.delete(video);
1455
+ }
1456
+ window.clearTimeout(giveUpTimeout);
1457
+ video.removeEventListener("playing", playingListener);
1458
+ video.muted = wasMuted;
1459
+ };
1460
+ // Let real-playback initiators cancel this priming attempt so it can't
1461
+ // mute or pause the playback they are about to start.
1462
+ const cancel = () => {
1463
+ finish();
1464
+ // Real playback is starting; make sure our transparent poster can't
1465
+ // hide it.
1466
+ video.removeAttribute("poster");
1467
+ };
1468
+ firstFramePrimingCancels.set(video, cancel);
1469
+ const playingListener = () => {
1470
+ hideVideoAutoplayBlockedHint(video);
1471
+ if (!shouldPauseAfterPlaying()) {
1472
+ // Real playback has taken over; restore audio and get out of the way.
1473
+ finish();
1474
+ video.removeAttribute("poster");
1420
1475
  return;
1421
1476
  }
1422
- const playingListener = () => {
1423
- window.clearTimeout(removePlayingListenerTimeout);
1424
- hideVideoAutoplayBlockedHint(video);
1425
- if (!shouldPauseAfterPlaying()) {
1426
- return;
1477
+ const pauseIfStillWanted = () => {
1478
+ if (done) {
1479
+ return; // we already gave up or something else took over
1427
1480
  }
1428
- const pauseIfStillWanted = () => {
1429
- if (shouldPauseAfterPlaying()) {
1430
- video.pause();
1431
- }
1432
- };
1433
- // Prefer pausing after a composited video frame, not just after a timer.
1434
- // Some videos/devices can report "playing" before pixels are actually painted.
1435
- const requestVideoFrameCallback = (video as any)
1436
- .requestVideoFrameCallback as
1437
- | ((callback: (...args: any[]) => void) => number)
1438
- | undefined;
1439
- if (requestVideoFrameCallback) {
1440
- requestVideoFrameCallback.call(video, () => {
1441
- pauseIfStillWanted();
1442
- });
1443
- } else {
1444
- // Fallback for older browsers. This sometimes helps, but is not as reliable as the
1445
- // above on browsers that implement it.
1446
- setTimeout(() => {
1447
- pauseIfStillWanted();
1448
- }, 4);
1481
+ if (shouldPauseAfterPlaying()) {
1482
+ video.pause();
1449
1483
  }
1484
+ finish();
1485
+ // A frame has been painted; the transparent poster (set at book
1486
+ // load to hide videos until then) is no longer wanted.
1487
+ video.removeAttribute("poster");
1450
1488
  };
1451
- video.addEventListener("playing", playingListener, { once: true });
1452
- // If our play() attempt doesn't lead to playback soon, remove the
1453
- // listener so it can't affect unrelated later playback.
1454
- const removePlayingListenerTimeout = window.setTimeout(() => {
1455
- video.removeEventListener("playing", playingListener);
1456
- }, 1000);
1457
- hideVideoAutoplayBlockedHint(video);
1458
- const promise = video.play();
1459
- if (promise && promise.catch) {
1460
- promise.catch((reason) => {
1461
- window.clearTimeout(removePlayingListenerTimeout);
1462
- // If play() fails (e.g., autoplay policy), remove the listener
1463
- // so it won't interfere if playback starts later by other means.
1464
- video.removeEventListener("playing", playingListener);
1465
- // If autoplay is blocked, remove our transparent poster so a decoded
1466
- // first frame can be shown when available.
1467
- if (reason?.name === "NotAllowedError") {
1468
- video.removeAttribute("poster");
1469
- onAutoplayBlocked();
1470
- }
1489
+ // Prefer pausing after a composited video frame, not just after a timer.
1490
+ // Some videos/devices can report "playing" before pixels are actually painted.
1491
+ const requestVideoFrameCallback = (video as any)
1492
+ .requestVideoFrameCallback as
1493
+ | ((callback: (...args: any[]) => void) => number)
1494
+ | undefined;
1495
+ if (requestVideoFrameCallback) {
1496
+ requestVideoFrameCallback.call(video, () => {
1497
+ pauseIfStillWanted();
1471
1498
  });
1499
+ } else {
1500
+ // Fallback for older browsers. This sometimes helps, but is not as reliable as the
1501
+ // above on browsers that implement it.
1502
+ setTimeout(() => {
1503
+ pauseIfStillWanted();
1504
+ }, 4);
1472
1505
  }
1473
1506
  };
1474
- // HAVE_CURRENT_DATA (2) means at least the first frame is decoded.
1475
- if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
1476
- attempt();
1477
- } else {
1478
- let cancelAttempt: () => void;
1479
- const onLoadedData = () => {
1480
- video.removeEventListener("play", cancelAttempt);
1481
- attempt();
1482
- };
1483
- cancelAttempt = () => {
1484
- canceled = true;
1485
- video.removeEventListener("loadeddata", onLoadedData);
1486
- video.removeEventListener("play", cancelAttempt);
1487
- };
1488
- // If something requests playback before the video has loaded enough data,
1489
- // don't run this first-frame trick when loadeddata eventually fires.
1490
- video.addEventListener("play", cancelAttempt, { once: true });
1491
- video.addEventListener("loadeddata", onLoadedData, { once: true });
1507
+ video.addEventListener("playing", playingListener, { once: true });
1508
+ // If nothing has painted after a while (very slow data, or a policy we
1509
+ // didn't anticipate), give up: cancel our pending muted playback, restore
1510
+ // audio, and remove the transparent poster so the browser can show its own
1511
+ // first frame when it has one, rather than leaving the video invisible.
1512
+ giveUpTimeout = window.setTimeout(() => {
1513
+ if (shouldPauseAfterPlaying() && !video.paused) {
1514
+ video.pause();
1515
+ }
1516
+ finish();
1517
+ video.removeAttribute("poster");
1518
+ }, 3000);
1519
+ hideVideoAutoplayBlockedHint(video);
1520
+ video.muted = true;
1521
+ const promise = video.play();
1522
+ if (promise && promise.catch) {
1523
+ promise.catch((reason) => {
1524
+ finish();
1525
+ // If autoplay is blocked even muted, remove our transparent poster
1526
+ // so a decoded first frame can be shown when available.
1527
+ if (reason?.name === "NotAllowedError") {
1528
+ video.removeAttribute("poster");
1529
+ onAutoplayBlocked();
1530
+ }
1531
+ });
1492
1532
  }
1493
1533
  }
1494
1534
 
@@ -1558,6 +1598,9 @@ function playAllVideoInternal(
1558
1598
  hideVideoError(video);
1559
1599
  hideVideoAutoplayBlockedHint(video);
1560
1600
  setCurrentPlaybackMode(PlaybackMode.VideoPlaying);
1601
+ // This is real playback; a pending first-frame priming attempt must
1602
+ // not mute or pause it.
1603
+ cancelVideoFirstFramePriming(video);
1561
1604
  // Always play each queued video from the beginning.
1562
1605
  // Without this, a previously played element may remain at end-of-stream
1563
1606
  // and fail to raise the expected ended event for sequencing.