OLD | NEW |
(Empty) | |
| 1 # Autoplay of HTMLMediaElements |
| 2 |
| 3 Autoplay is the concept of playing media elements without user gesture. On |
| 4 desktop, autoplay is always allowed. On mobile, only muted video elements are |
| 5 allowed to autoplay. The autoplay logic follows |
| 6 the |
| 7 [HTML spec](https://html.spec.whatwg.org/multipage/embedded-content.html#media-e
lements). |
| 8 |
| 9 There are two ways of initiating autoplay: |
| 10 |
| 11 * Autoplay by attribute: Setting the `autoplay` attribute on the media element. |
| 12 The element will try to autoplay when the `readyState` changes to |
| 13 HAVE_ENOUGH_DATA. |
| 14 * Autoplay by `play()` method: Explicitly calling the `play()` method without |
| 15 user gesture. |
| 16 |
| 17 ## User gesture lock |
| 18 |
| 19 Each media element has a user gesture lock. If the element is allowed to |
| 20 autoplay, the lock is initialized as `false`, otherwise it's `true`. |
| 21 |
| 22 When the element is trying to initate autoplay, we check the gesture lock. If |
| 23 the lock is `false`, it will be allowed. Otherwise autoplay will be blocked. An |
| 24 exception is that if the element is a muted video element, the gesture lock |
| 25 check will be bypassed. |
| 26 |
| 27 To unlock the gesture lock (make it `false`). The page needs to call play() or |
| 28 load() on the element when responding to a user gesture. |
| 29 |
| 30 ## Autoplay flowchart |
| 31 |
| 32 The treatments of autoplay by different methods are different. For autoplay by |
| 33 attribute, it is: |
| 34 |
| 35  |
| 36 |
| 37 This means if autoplay is initiated by attribute, it enters the autoplaying |
| 38 phase, we play it as long as the video is visible. When the page explicitly |
| 39 calls `play()`, `pause()`, we leave the autoplaying phase. When the page tries |
| 40 to unmute the video, we check the gesture lock and pause the video if it is |
| 41 still `true`. |
| 42 |
| 43 For autoplay by `play()` method, it is: |
| 44 |
| 45  |
| 46 |
| 47 This means if autoplay is initiated by `play()` method, we continue playing the |
| 48 video as normal `play()`. However if the page tries to unmute the video, we chec
k |
| 49 the gesture lock and pause the video if it is still `true`. |
OLD | NEW |