Skip to content

Commit

Permalink
Adding documentation for autoplay
Browse files Browse the repository at this point in the history
BUG=None

Review-Url: https://codereview.chromium.org/2787513002
Cr-Commit-Position: refs/heads/master@{#460722}
  • Loading branch information
xxyzzzq authored and Commit bot committed Mar 30, 2017
1 parent 238aa72 commit 4b8096b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
File renamed without changes.
49 changes: 49 additions & 0 deletions docs/media/autoplay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Autoplay of HTMLMediaElements

Autoplay is the concept of playing media elements without user gesture. On
desktop, autoplay is always allowed. On mobile, only muted video elements are
allowed to autoplay. The autoplay logic follows
the
[HTML spec](https://html.spec.whatwg.org/multipage/embedded-content.html#media-elements).

There are two ways of initiating autoplay:

* Autoplay by attribute: Setting the `autoplay` attribute on the media element.
The element will try to autoplay when the `readyState` changes to
HAVE_ENOUGH_DATA.
* Autoplay by `play()` method: Explicitly calling the `play()` method without
user gesture.

## User gesture lock

Each media element has a user gesture lock. If the element is allowed to
autoplay, the lock is initialized as `false`, otherwise it's `true`.

When the element is trying to initate autoplay, we check the gesture lock. If
the lock is `false`, it will be allowed. Otherwise autoplay will be blocked. An
exception is that if the element is a muted video element, the gesture lock
check will be bypassed.

To unlock the gesture lock (make it `false`). The page needs to call play() or
load() on the element when responding to a user gesture.

## Autoplay flowchart

The treatments of autoplay by different methods are different. For autoplay by
attribute, it is:

![Autoplay by attribute](autoplay_by_attribute.png)

This means if autoplay is initiated by attribute, it enters the autoplaying
phase, we play it as long as the video is visible. When the page explicitly
calls `play()`, `pause()`, we leave the autoplaying phase. When the page tries
to unmute the video, we check the gesture lock and pause the video if it is
still `true`.

For autoplay by `play()` method, it is:

![Autoplay by play() method](autoplay_by_play_method.png)

This means if autoplay is initiated by `play()` method, we continue playing the
video as normal `play()`. However if the page tries to unmute the video, we check
the gesture lock and pause the video if it is still `true`.
Binary file added docs/media/autoplay_by_attribute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/media/autoplay_by_play_method.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4b8096b

Please sign in to comment.