Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play() must not fail here: HTMLMediaElement.remove(); el.play() #9467

Open
WofWca opened this issue Jun 29, 2023 · 4 comments
Open

play() must not fail here: HTMLMediaElement.remove(); el.play() #9467

WofWca opened this issue Jun 29, 2023 · 4 comments

Comments

@WofWca
Copy link

WofWca commented Jun 29, 2023

https://html.spec.whatwg.org/multipage/media.html#media-playback

When a media element is removed from a Document, the user agent must run the following steps:​

Await a stable state, allowing the task that removed the media element from the Document to continue. The synchronous section consists of all the remaining steps of this algorithm. (Steps in the synchronous section are marked with ⌛.)

⌛ If the media element is in a document, return.

⌛ Run the internal pause steps for the media element.

POC code:

const v = document.querySelector('video');
v.pause(); // It may already be paused, but ensure it
v.remove();
v.play();

Expected behavior: the element starts playing
Actual behavior: the element doesn't play, v.paused === true.

Workaround: execute v.play() on setTimeout or queueMicrotask.

I understand that we want elements to get paused when they get removed from the DOM, but this pauses them a little while after they get removed.

Suggested, not thoroughly thought through solution: when play() is called, cancel the abovementioned scheduled "pause" task.

There is an MR #7855 that touches this piece of the spec, but it still doesn't address the issue.

For context, I discovered this while working on this MR.

@annevk
Copy link
Member

annevk commented Jun 30, 2023

cc @whatwg/media

@foolip
Copy link
Member

foolip commented Sep 15, 2023

@WofWca have you found a difference between browsers with your example, or do they all pause?

What you suggest (removing the scheduled task) actually seems like the perfect solution to this, if we were to change the spec.

The "If the media element is in a document" check is there to avoid pausing if the element was moved to another document, so there's already some precedent for "canceling" the pause based on what happens after removing from a document.

@dalecurtis WDYT?

@WofWca
Copy link
Author

WofWca commented Sep 15, 2023

have you found a difference between browsers with your example, or do they all pause?

Desktop Chromium and Gecko (Firefox) both pause, I didn't check other ones.

@dalecurtis
Copy link
Contributor

dalecurtis commented Sep 26, 2023

Seems reasonable. At first glance I thought this would need to undo a bunch of the load algorithm hijinks, but it just needs to cancel the removed from document timer: https://chromium-review.googlesource.com/c/chromium/src/+/4895097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants