mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(player): unload the video when its src is cleared (#7466)
* fix(player): unload the video when its src is cleared * docs(player): keep source-reset guidance within the package budget
This commit is contained in:
@@ -81,6 +81,8 @@ With a `video/...` `type`, `src` plays in a `<video>` behind the same playback A
|
||||
events. `scenes`, `setRuntimeData()`, `setColorGrading()`, `iframeElement` and `stopMedia()`
|
||||
are composition-only, and `srcdoc` wins over a video `src`, as it does in an iframe.
|
||||
|
||||
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.
|
||||
|
||||
## JavaScript API
|
||||
|
||||
The main API follows familiar media-player behavior:
|
||||
|
||||
@@ -53,6 +53,8 @@ Set `type` to a video type and the player plays `src` in a `<video playsinline>`
|
||||
></hyperframes-player>
|
||||
```
|
||||
|
||||
Removing or emptying `src` unloads the video and pauses it at 0:00.
|
||||
|
||||
### Poster image
|
||||
|
||||
Show a static image before playback starts:
|
||||
|
||||
@@ -3350,6 +3350,34 @@ describe("HyperframesPlayer video mode", () => {
|
||||
expect(withControls.duration).toBe(0);
|
||||
});
|
||||
|
||||
it.each([null, ""])("unloads a playing video when src is cleared to %s", (src) => {
|
||||
const withControls = createPlayer({ type: "video/mp4", src: FILM, controls: "" });
|
||||
const video = videoOf(withControls);
|
||||
loadMetadata(video);
|
||||
withControls.seek(3);
|
||||
withControls.play();
|
||||
|
||||
if (src === null) withControls.removeAttribute("src");
|
||||
else withControls.setAttribute("src", src);
|
||||
|
||||
expect(video.getAttribute("src")).toBeNull();
|
||||
expect(HTMLMediaElement.prototype.load).toHaveBeenCalledOnce();
|
||||
expect(withControls.ready).toBe(false);
|
||||
expect(withControls.paused).toBe(true);
|
||||
expect(withControls.currentTime).toBe(0);
|
||||
expect(withControls.duration).toBe(0);
|
||||
expect(withControls.shadowRoot?.textContent).toContain("0:00 / 0:00");
|
||||
expect(withControls.shadowRoot?.querySelector('[aria-label="Play"]')).not.toBeNull();
|
||||
|
||||
withControls.setAttribute("src", FILM);
|
||||
loadMetadata(video);
|
||||
withControls.play();
|
||||
|
||||
expect(withControls.ready).toBe(true);
|
||||
expect(withControls.paused).toBe(false);
|
||||
expect(video.getAttribute("src")).toBe(FILM);
|
||||
});
|
||||
|
||||
it("shows a playback rate configured before controls are created", () => {
|
||||
player = createPlayer({ type: "video/mp4", src: FILM, "playback-rate": "2", controls: "" });
|
||||
|
||||
|
||||
@@ -279,7 +279,7 @@ class HyperframesPlayer extends HTMLElement {
|
||||
// message fire before connectedCallback installs the parent message listener. Initial
|
||||
// attributes are applied below by connectedCallback; only live changes navigate here.
|
||||
if (!this.isConnected) break;
|
||||
if (val) this._navigateSrc(val);
|
||||
if (val || this._videoSource) this._navigateSrc(val ?? "");
|
||||
break;
|
||||
case "type": {
|
||||
const src = this.getAttribute("src");
|
||||
@@ -1100,7 +1100,11 @@ class HyperframesPlayer extends HTMLElement {
|
||||
this.controlsApi?.updateTime(0, 0);
|
||||
this._syncVideoAudio();
|
||||
adapter.timeScale?.(this.playbackRate);
|
||||
video.src = src;
|
||||
if (src) video.src = src;
|
||||
else {
|
||||
video.removeAttribute("src");
|
||||
video.load();
|
||||
}
|
||||
}
|
||||
|
||||
private _teardownVideo(): void {
|
||||
|
||||
Reference in New Issue
Block a user