OLD | NEW |
1 # Autoplay of HTMLMediaElements | 1 # Autoplay of HTMLMediaElements |
2 | 2 |
3 Autoplay is the concept of playing media elements without user gesture. On | 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 | 4 desktop, autoplay is always allowed. On mobile, only muted video elements are |
5 allowed to autoplay. The autoplay logic follows | 5 allowed to autoplay. The autoplay logic follows |
6 the | 6 the |
7 [HTML spec](https://html.spec.whatwg.org/multipage/embedded-content.html#media-e
lements). | 7 [HTML spec](https://html.spec.whatwg.org/multipage/embedded-content.html#media-e
lements). |
8 | 8 |
9 There are two ways of initiating autoplay: | 9 There are two ways of initiating autoplay: |
10 | 10 |
11 * Autoplay by attribute: Setting the `autoplay` attribute on the media element. | 11 * Autoplay by attribute: Setting the `autoplay` attribute on the media element. |
12 The element will try to autoplay when the `readyState` changes to | 12 The element will try to autoplay when the `readyState` changes to |
13 HAVE_ENOUGH_DATA. | 13 HAVE_ENOUGH_DATA. |
14 * Autoplay by `play()` method: Explicitly calling the `play()` method without | 14 * Autoplay by `play()` method: Explicitly calling the `play()` method without |
15 user gesture. | 15 user gesture. |
16 | 16 |
| 17 All the autoplay logic is handled by the AutoplayPolicy class. When the media |
| 18 element wants to perform some action (like unmute, autoplay by attribute or |
| 19 `play()` method), it will send a request to AutoplayPolicy, and if the request |
| 20 is approved, the element can autoplay, otherwise it should be paused. Also the |
| 21 media element should inform the AutoplayPolicy about relevant changes such as |
| 22 "the element has been moved to a new document". |
| 23 |
17 ## User gesture lock | 24 ## User gesture lock |
18 | 25 |
19 Each media element has a user gesture lock. If the element is allowed to | 26 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`. | 27 autoplay, the lock is initialized as `false`, otherwise it's `true`. |
21 | 28 |
22 When the element is trying to initate autoplay, we check the gesture lock. If | 29 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 | 30 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 | 31 exception is that if the element is a muted video element, the gesture lock |
25 check will be bypassed. | 32 check will be bypassed. |
26 | 33 |
(...skipping 13 matching lines...) Expand all Loading... |
40 to unmute the video, we check the gesture lock and pause the video if it is | 47 to unmute the video, we check the gesture lock and pause the video if it is |
41 still `true`. | 48 still `true`. |
42 | 49 |
43 For autoplay by `play()` method, it is: | 50 For autoplay by `play()` method, it is: |
44 | 51 |
45  | 52  |
46 | 53 |
47 This means if autoplay is initiated by `play()` method, we continue playing the | 54 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 | 55 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`. | 56 the gesture lock and pause the video if it is still `true`. |
OLD | NEW |