2.2.8

Lifecycle Events

Last updated


This page covers when each of the five when callbacks fires, which underlying event triggers it per source, and the timing limit inside ready.

Callbacks at a Glance


CallbackHTML5 video/audioYouTubeVimeoWhat FlexPlyr also does
readyloadedmetadataonReadyready() resolvesSets duration, resets progress, applies volume and mute
playingplayingstate 1playStarts the 100 ms progress poll; hides the panel after 1 s
pausepausestate 2pauseStops the poll and shows the panel
endendedstate 0endedStops the poll, resets the panel, seeks back to 0
destroyedAfter destroy() removes the DOMSameSame—

The legacy event object still works as an alias of when (see Deprecations).

Example


Click play, then pause, then destroy(); the log below lists each callback as it fires:
  1. <div style="max-width: 480px; aspect-ratio: 16 / 9"><div id="player"></div></div>
  2. <button id="destroy">destroy()</button>
  3. <script>
  4. const player = new FPlyr({
  5. id: "player",
  6. video: "https://cdn.pixabay.com/video/2023/11/28/191159-889246512_tiny.mp4",
  7. when: {
  8. ready: () => console.log("ready"),
  9. playing: () => console.log("playing"),
  10. pause: () => console.log("pause"),
  11. end: () => console.log("end"),
  12. destroyed: () => console.log("destroyed"),
  13. },
  14. });
  15. document.getElementById("destroy").onclick = () => player.destroy();
  16. </script>
Live preview · @pardnchiu/flexplyr@2.2.9

ready Runs Before Internal State


ready is the first line of #stateReady() (src/js/model/player.js:397), before the #isVideo/#isYoutube source flags are set (:406). Every public method branches on those flags, so synchronous calls inside ready miss:
  1. <div style="max-width: 480px; aspect-ratio: 16 / 9"><div id="player"></div></div>
  2. <script>
  3. const player = new FPlyr({
  4. id: "player",
  5. video: "https://cdn.pixabay.com/video/2023/11/28/191159-889246512_tiny.mp4",
  6. when: {
  7. ready: () => {
  8. console.log("inside ready:", String(player.isPaused()));
  9. setTimeout(() => console.log("next tick:", player.isPaused()));
  10. },
  11. },
  12. });
  13. </script>
Live preview · @pardnchiu/flexplyr@2.2.9

To act on the player at readiness, wrap the call in setTimeout.

中文