Skip to content

Commit

Permalink
Merge pull request #182 from GoogleChrome/update/feature-flags-and-ne…
Browse files Browse the repository at this point in the history
…w-posts

New User Experience and Enhancement posts.
  • Loading branch information
dero committed Apr 19, 2022
2 parents 14d0740 + d3e0321 commit 1d5d48d
Show file tree
Hide file tree
Showing 15 changed files with 1,539 additions and 85 deletions.
16 changes: 11 additions & 5 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,15 @@ main {
* Single Video
*/
article {
width: 100%;
box-sizing: border-box;
padding-bottom: calc(var(--gutter) * 3);
margin-bottom: calc(var(--gutter) * 3);
border-bottom: 1px solid var(--separator);
overflow: hidden;
}
article .video-content {
width: 100%;
max-width: 760px;
margin-left: auto;
margin-right: auto;
Expand Down Expand Up @@ -613,28 +617,30 @@ h3[id*="example"]{
font-size: 24px;
margin-bottom: calc(var(--gutter) / 2);
}
article pre,
article pre code,
.code-sample .code-sample--content {
max-width: 760px;
white-space: pre-wrap;
display: block;
font-size: 18px;
font-family: monospace;
padding: var(--gutter);
border: 1px solid var(--code-border);
background: var(--code-background);
overflow-y: auto;
overflow-x: auto;
}
.video-container {
position: relative;
border-radius: 8px;
max-width: 1200px;
margin: 0 auto;
overflow: hidden;
aspect-ratio: 16 / 9;
padding-bottom: 56.25%;
}
.video-container--overlay {
display: none;
}
.video-container.has-player {
padding-bottom: 0;
}
.has-overlay .video-container--overlay {
position: absolute;
width: 100%;
Expand Down
2 changes: 0 additions & 2 deletions src/api/01-basics/01-single-video.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ length: '1:04'
video-sources:
- src: https://storage.googleapis.com/kino-assets/single-video/video.mp4
type: video/mp4; codecs="avc1.640032,mp4a.40.2"
cast: true
thumbnail: https://storage.googleapis.com/kino-assets/single-video/thumbnail.png
media-session-artwork:
- sizes: 96x96
Expand All @@ -29,7 +28,6 @@ media-session-artwork:
- sizes: 512x512
src: https://storage.googleapis.com/kino-assets/single-video/artwork-512x512.png
type: image/png

---

## Introduction
Expand Down
225 changes: 225 additions & 0 deletions src/api/03-user-experience/07-autoplay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
---
title: Autoplay
description: |
Learn how to start video playback automatically and employ autoplay strategies that don't lead to degraded user experience.
date: Septembed 22nd, 2021
length: '1:04'
video-sources:
- src: https://storage.googleapis.com/kino-assets/autoplay/manifest.mpd
type: application/dash+xml
- src: https://storage.googleapis.com/kino-assets/autoplay/master.m3u8
type: application/x-mpegURL
video-subtitles:
- default: true
kind: captions
label: English
src: https://storage.googleapis.com/kino-assets/autoplay/cap-en.vtt
srclang: en
- default: false
kind: captions
label: Česky
src: https://storage.googleapis.com/kino-assets/autoplay/cap-cs.vtt
srclang: cz
thumbnail: https://storage.googleapis.com/kino-assets/autoplay/thumbnail.png
media-session-artwork:
- sizes: 96x96
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-96x96.png
type: image/png
- sizes: 128x128
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-128x128.png
type: image/png
- sizes: 192x192
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-192x192.png
type: image/png
- sizes: 256x256
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-256x256.png
type: image/png
- sizes: 384x384
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-384x384.png
type: image/png
- sizes: 512x512
src: https://storage.googleapis.com/kino-assets/autoplay/artwork-512x512.png
type: image/png
autoplay: true
---

## Introduction

Sometimes the video is not just a _part_ of your web page content. Sometimes it
is _the_ content and your users expect it to start playing automatically
as soon as possible.

There are two main ways of accomplishing this autoplay behavior:

1. Using the `<video autoplay>` attribute.
2. Using JavaScript.

Let's take a look at the benefits and limitations of each.

## Using the `autoplay` attribute

The better known and simpler way of instructing browsers to automatically play
a video is using the `autoplay` attribute.

```html
<video controls autoplay>
<source src="video.mp4" type="video/mp4">
</video>
```

Browsers might not honor the attribute for various reasons, likely
because a given video contains sound. Automatic playback of videos with sound often leads
to poor user experience and browsers use multiple signals to determine
if playback with sound should be allowed.

To improve the chance of browsers starting playback, choose to
play the video muted.

```html
<!-- Browsers will usually allow autoplay when
the `muted` attribute is also present. -->
<video controls autoplay muted>
<source src="video.mp4" type="video/mp4">
</video>
```

**Note:** Unless you provide your own UI for the video controls, make sure
to also use the `controls` attribute to let the browser render the default video
controls.

## Using JavaScript

In JavaScript the `<video>` element contains a `play()` method that can be used
to attempt to manually start playback as soon as possible.

```html
<video controls>
<source src="video.mp4" type="video/mp4">
</video>
<script>
const videoElement = document.querySelector('video');
videoElement.play().then(() => { /* video is playing */});
</script>
```

As with the `autoplay` attribute, browsers may choose to not play the
video in this situation, especially if it contains sound and is not muted.

One benefit of calling the `play()` method is that it returns a promise
that resolves when playback starts and rejects when it won't start for any
reason. This gives you a chance to respond.

A good strategy is to mute the video and re-attempt the playback. If
that fails, render a custom play button or don't do anything and let the user use video controls rendered by the browser.

```html
<video controls>
<source src="video.mp4" type="video/mp4">
</video>
<script>
const videoElement = document.querySelector('video');
videoElement.play()
.catch(() => {
// Mute video and retry playback.
videoElement.muted = true;
videoElement.play()
.catch(() => {
// Can't autoplay with sound or muted.
})
})
/**
* Note: In older browsers the `play()` method may not
* return a promise. If you need to support
* pre-2019 browsers, you could do:
*
* Promise.resolve(videoElement.play())
* .catch(() => {
* // Mute and try playing again.
* })
*
* ... to wrap the non-promise return values as promises.
</script>
```

### Unmute button

Refresh this page and you'll likely notice the video now plays muted and there
is a "Tap to unmute" button rendered over it.

That's a custom element styled and positioned to render over the video. When
the button is clicked, simple JavaScript runs and removes the `muted` attribute
from the video and it continues playing with sound.

This strategy combines the best of both worlds. Users are not going to get
startled by sudden loud playback while the action necessary to achieve playback
with sound is handily available.

```html
<section>
<video controls>
<source src="video.mp4" type="video/mp4">
</video>
<button>🔊</button>
</section>

<style>
/* Only basic layout, no fancy styling. */
section {
display: inline-block;
position: relative;
}
button {
font-size: 80px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>

<script>
const videoElement = document.querySelector('video');
const unmuteButton = document.querySelector('button');
videoElement.play()
.catch(() => {
// Mute video and retry playback.
videoElement.muted = true;
videoElement.play()
.then(() => {
// When the unmute button is clicked,
// unmute video and remove the button.
unmuteButton.addEventListener('click', () => {
videoElement.muted = false;
unmuteButton.remove();
});
})
.catch(() => {
// Can't autoplay with sound or muted.
})
})
</script>
```

## Engagement signals

Browsers use various heuristics to determine whether automatic playback with
sound is expected by the user or not. These heuristics differ between browsers,
but here are a few example use cases where autoplay with sound is likely:

* When the newly loaded page is a result of internal navigation initiated by
the user, e.g. click, tap etc.
* When the user has installed the PWA or added the site to their home screen.
* When user engagement signals are strong, e.g. on video sites where the user
regularly watches the content.

Find more in-depth information at the [Chrome Developers website].

## What's Next?

In the [next article], we are going explore the Picture-in-Picture API, which can be used to play videos and other media sources in a floating window that stays on top of other applications.

[Chrome Developers website]: https://developer.chrome.com/blog/autoplay/
[next article]: /picture-in-picture/
Loading

0 comments on commit 1d5d48d

Please sign in to comment.