Player object for talking to it over postMessage. This guide builds a BunnyPlayer component with Svelte 5 runes that renders the iframe, forwards its events as callback props, and hands you that object.
The component also works under SvelteKit server rendering. player.js is imported in onMount, because it reads window when imported, and the iframe renders only after the library is available, because player.js has to be loaded before the iframe finishes loading.
SvelteKit example on GitHub
A SvelteKit app built around this component, with custom controls and an event log. It plays a demo video after
bun install and bun dev.Quickstart
1
Install player.js
tsconfig.json includes, for example player.js.d.ts. It covers the methods and events the Bunny Player supports:player.js.d.ts
2
Create the component
src/lib/components/BunnyPlayer.svelte
{#key src} replaces the iframe when the video changes, so the effect runs again with a fresh Player. The callback props are read inside the event handlers, so passing a new inline function doesn’t re-create the player.3
Render a video
Load the video ID in The library ID already appears in every embed URL, so a
+page.server.ts and pass it through data:src/routes/lessons/[id]/+page.svelte
PUBLIC_ variable is fine. params accepts any player parameter, such as captions, t, or muted.Control playback
onready hands you the Player. Keep it in state and call its methods from your own controls:
player.js package on npm (0.1.0) has no playback speed method, so send the command directly. The build bunny.net hosts adds setPlaybackRate(), getPlaybackRate() and the playbackratechange event from version 0.1.2; see Methods.
play() until the viewer has interacted with the page, so call mute() first if playback has to start without a click. Playback control API lists every method and event.
Track progress
timeupdate fires several times a second with { seconds, duration }. Throttle it before writing to your backend, for example through an API route:
t parameter to resume from there.
Load player.js from the CDN instead
To skip the npm dependency, load the hosted build insrc/app.html:
src/app.html
onMount with playerjs = window.playerjs and declare the global in src/app.d.ts:
src/app.d.ts
Troubleshooting
ReferenceError: window is not defined
ReferenceError: window is not defined
player.js is being imported during server rendering. Keep
import("player.js") inside onMount. A static import playerjs from "player.js" at the top of the script block runs on the server in SvelteKit.onready never fires
onready never fires
The iframe finished loading before player.js was loaded. Render the iframe only after player.js has loaded, as the component above does. A
Player created after that still connects, because player.js caches the iframe’s ready message.ready is also delayed while the tab is hidden, and fires once it becomes visible.Events fire twice after changing the video
Events fire twice after changing the video
Every
Player adds a message listener to window that is never removed. The active flag in the effect cleanup keeps stale instances quiet, so check that yours flips it.The iframe shows a 403
The iframe shows a 403
The library’s allowed domains, direct access block, or token authentication is rejecting the embed. See Embedding restrictions.