W3C home > Mailing lists > Public > whatwg@whatwg.org > October 2007

[whatwg] When to stop <video> elements from playing

From: Jonas Sicking <jonas@sicking.cc>
Date: Fri, 19 Oct 2007 15:09:02 -0700
Message-ID: <47192AFE.2020003@sicking.cc>
Robert O'Callahan wrote:
> On Oct 19, 2007 11:55 AM, Geoffrey Garen <ggaren at apple.com 
> <mailto:ggaren at apple.com>> wrote:
> 
>      > Suppose a script creates a <video> element, adds it to the document,
>      > starts it playing, then removes the element from the document and
>      > drops all references to it. When should the element stop playing?
> 
>      > -- when the element leaves the document?
> 
>     Probably. Since you can't see the video any more, it would be really
>     weird to hear audio from it, or waste computer resources on a mute and
>     invisible video. Moreover, there's a lot of precedent for DOM elements
>     not loading when they're not in the document. This is true of iframe,
>     script, img, etc.
> 
> 
> It's not true of <img>. Javascript image preloading tricks rely on it.
> 
> And as Maciej mentioned, it would be useful to be able to play audio 
> outside the document, and to be consistent, <video>'s behaviour should 
> match.

Image is one of very few exceptions to the rule that elements become 
active only when they are inserted into the document. And partially this 
is due to DOM0 behavior that was designed before there was an actual DOM 
you could insert your images into. I.e. it stems from people using the 
following syntax:

myimg = new Image();
myimg.src = "myimg.gif";

function showImg() {
   document.images[5].src = "myimg.gif";
}

So I don't think it's a precedent we need to follow. There is much more 
precedent for only making elements active once they are inserted into 
the document.

>      > -- when all JS references to the element have been dropped (and
>      > garbage collection runs)?
> 
>     No. The time at which garbage collection will reclaim an object is
>     unpredictable, so relying on garbage collection for behavior is a bad
>     idea.
> 
> Agreed.

If we let <video>s or <audio>s play even when out of the document I 
don't see how we could avoid this. Or do you mean by 'play' only move 
forward in their time-position, not emit sound?

What happens in opera is if you do:

myAudio = new Audio("foo.wav");
myAudio.onload = function () {
   this.play();
   myAudio = null;
}

When will the audio stop playing?

> My current opinion is that <audio> and <video> elements should behave 
> like <img> and load/play whether or not they're in a document, but they 
> should only emit sound if they're in a document with a presentation and 
> are not in a display:none subtree. Then to play a sound you'd have to 
> insert the element into your document somewhere with size 0x0 (which 
> should be the default for <audio>).
> 
> But it's a rather tangled issue.

It would make sense to me if we started loads as soon as the 
<audio>/<video> was created. I'm much less convinced that elements out 
the the DOM should emit sound. Though the API anne describes of using 
pure JS objects does make a lot of sense.

/ Jonas
Received on Friday, 19 October 2007 15:09:02 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:37 UTC