Skip to content

Commit dbb389c

Browse files
authored
fix(player): unload the video when its src is cleared
1 parent 7bf19ab commit dbb389c

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎docs/packages/player.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ With a `video/...` `type`, `src` plays in a `<video>` behind the same playback A
8181
events. `scenes`, `setRuntimeData()`, `setColorGrading()`, `iframeElement` and `stopMedia()`
8282
are composition-only, and `srcdoc` wins over a video `src`, as it does in an iframe.
8383

84+
Removing a video player's `src` attribute or setting it to an empty string unloads the film and resets playback to paused at 0:00. Set a new `src` to load another video.
85+
8486
## JavaScript API
8587

8688
The main API follows familiar media-player behavior:

‎packages/player/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Set `type` to a video type and the player plays `src` in a `<video playsinline>`
5353
></hyperframes-player>
5454
```
5555

56+
Removing a video player's `src` attribute or setting it to an empty string unloads the film and resets playback to paused at 0:00. Set a new `src` to load another video.
57+
5658
### Poster image
5759

5860
Show a static image before playback starts:

‎packages/player/src/hyperframes-player.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,34 @@ describe("HyperframesPlayer video mode", () => {
33503350
expect(withControls.duration).toBe(0);
33513351
});
33523352

3353+
it.each([null, ""])("unloads a playing video when src is cleared to %s", (src) => {
3354+
const withControls = createPlayer({ type: "video/mp4", src: FILM, controls: "" });
3355+
const video = videoOf(withControls);
3356+
loadMetadata(video);
3357+
withControls.seek(3);
3358+
withControls.play();
3359+
3360+
if (src === null) withControls.removeAttribute("src");
3361+
else withControls.setAttribute("src", src);
3362+
3363+
expect(video.getAttribute("src")).toBeNull();
3364+
expect(HTMLMediaElement.prototype.load).toHaveBeenCalledOnce();
3365+
expect(withControls.ready).toBe(false);
3366+
expect(withControls.paused).toBe(true);
3367+
expect(withControls.currentTime).toBe(0);
3368+
expect(withControls.duration).toBe(0);
3369+
expect(withControls.shadowRoot?.textContent).toContain("0:00 / 0:00");
3370+
expect(withControls.shadowRoot?.querySelector('[aria-label="Play"]')).not.toBeNull();
3371+
3372+
withControls.setAttribute("src", FILM);
3373+
loadMetadata(video);
3374+
withControls.play();
3375+
3376+
expect(withControls.ready).toBe(true);
3377+
expect(withControls.paused).toBe(false);
3378+
expect(video.getAttribute("src")).toBe(FILM);
3379+
});
3380+
33533381
it("shows a playback rate configured before controls are created", () => {
33543382
player = createPlayer({ type: "video/mp4", src: FILM, "playback-rate": "2", controls: "" });
33553383

‎packages/player/src/hyperframes-player.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class HyperframesPlayer extends HTMLElement {
279279
// message fire before connectedCallback installs the parent message listener. Initial
280280
// attributes are applied below by connectedCallback; only live changes navigate here.
281281
if (!this.isConnected) break;
282-
if (val) this._navigateSrc(val);
282+
if (val || this._videoSource) this._navigateSrc(val ?? "");
283283
break;
284284
case "type": {
285285
const src = this.getAttribute("src");
@@ -1100,7 +1100,11 @@ class HyperframesPlayer extends HTMLElement {
11001100
this.controlsApi?.updateTime(0, 0);
11011101
this._syncVideoAudio();
11021102
adapter.timeScale?.(this.playbackRate);
1103-
video.src = src;
1103+
if (src) video.src = src;
1104+
else {
1105+
video.removeAttribute("src");
1106+
video.load();
1107+
}
11041108
}
11051109

11061110
private _teardownVideo(): void {

0 commit comments

Comments
 (0)