将网页显示在附加的辅助显示屏上

弗朗索瓦·博福
François Beaufort

Chrome 66 允许网页通过 Presentation API 使用辅助连接的显示屏,并通过 Presentation Receiver API 控制其内容。

1 月 2 日。用户选择连接的辅助显示屏
1/2. 用户选择连接的辅助屏幕
2 月 2 日。网页会自动显示在先前选择的显示屏上
2/2. 网页会自动显示在先前选择的显示屏上

背景

到目前为止,Web 开发者可以打造这样一种体验:用户在 Chrome 中看到的本地内容与远程显示屏上的内容不同,同时仍能在本地控制这种体验。例如,在电视上播放视频时在 youtube.com 上管理播放队列,或者在环聊会话中全屏展示时,在笔记本电脑上看到带有演讲者备注的短片集。

但在某些情况下,用户可能只想在第二个连接的显示屏上呈现内容。例如,假设某位用户在会议室中,配备了一台投影仪,并通过 HDMI 线将其连接到该投影仪。用户实际上希望在投影仪上全屏演示幻灯片,而不是将演示文稿镜像到远程端点,从而使笔记本电脑屏幕可用于演讲者备注和幻灯片控制。虽然网站作者可以通过非常基本的方式(例如弹出一个新窗口,然后用户必须手动将其拖动到辅助显示屏并最大化至全屏)支持此操作,但这样做非常麻烦,并且会导致本地呈现和远程呈现之间的体验不一致。

展示页面

我将向您介绍如何使用 Presentation API 在连接辅助显示屏上呈现网页。您可在 https://googlechrome.github.io/samples/presentation-api/ 中找到最终结果。

首先,我们将创建一个新的 PresentationRequest 对象,其中包含我们要在连接的辅助显示屏上显示的网址。

const presentationRequest = new PresentationRequest('receiver.html');

In this article, I won’t cover use cases where the parameter passed to
`PresentationRequest` can be an array like `['cast://foo’, 'apple://foo',
'https://example.com']` as this is not relevant there.

We can now monitor presentation display availability and toggle a "Present"
button visibility based on presentation displays availability. Note that we can
also decide to always show this button.

<aside class="caution"><b>Caution:</b> The browser may use more energy while the <code>availability</code> object is alive
and actively listening for presentation display availability changes. Please
use it with caution in order to save energy on mobile.</aside>

```js
presentationRequest.getAvailability()
  .then(availability => {
    console.log('Available presentation displays: ' + availability.value);
    availability.addEventListener('change', function() {
      console.log('> Available presentation displays: ' + availability.value);
    });
  })
  .catch(error => {
    console.log('Presentation availability not supported, ' + error.name + ': ' +
        error.message);
  });

显示演示文稿显示提示需要用户手势,例如点击按钮。因此,我们在点击按钮时调用 presentationRequest.start(),并等待用户选择演示文稿屏幕(例如,在我们的用例中为辅助连接的屏幕)后,promise 会解析。

function onPresentButtonClick() {
  presentationRequest.start()
  .then(connection => {
    console.log('Connected to ' + connection.url + ', id: ' + connection.id);
  })
  .catch(error => {
    console.log(error);
  });
}

向用户显示的列表还可能包含远程端点(如果您连接到通告这些远程端点的网络)。请注意,镜像显示屏不在列表中。请参阅 http://crbug.com/840466

演示文稿显示选择器
演示文稿显示选择器

当 promise 解析后,PresentationRequest 对象网址中的网页将呈现给所选屏幕。大功告成!

现在,我们可以进一步监控“关闭”和“终止”事件,如下所示。请注意,您可以使用 presentationRequest.reconnect(presentationId) 重新连接到“封闭的”presentationConnection,其中 presentationId 是上一个 presentationRequest 对象的 ID。

function onCloseButtonClick() {
  // Disconnect presentation connection but will allow reconnection.
  presentationConnection.close();
}

presentationConnection.addEventListener('close', function() {
  console.log('Connection closed.');
});


function onTerminateButtonClick() {
  // Stop presentation connection for good.
  presentationConnection.terminate();
}

presentationConnection.addEventListener('terminate', function() {
  console.log('Connection terminated.');
});

与信息页通信

现在您在想,这很好,但是如何在我的控制器页面(我们刚刚创建的页面)和接收器页面(我们传递给 PresentationRequest 对象的页面)之间传递消息呢?

首先,我们使用 navigator.presentation.receiver.connectionList 检索接收器页面上的现有连接,并监听传入连接,如下所示。

// Receiver page

navigator.presentation.receiver.connectionList
.then(list => {
  list.connections.map(connection => addConnection(connection));
  list.addEventListener('connectionavailable', function(event) {
    addConnection(event.connection);
  });
});

function addConnection(connection) {

  connection.addEventListener('message', function(event) {
    console.log('Message: ' + event.data);
    connection.send('Hey controller! I just received a message.');
  });

  connection.addEventListener('close', function(event) {
    console.log('Connection closed!', event.reason);
  });
}

接收消息的连接会触发您可以监听的“message”事件。该消息可以是字符串、Blob、ArrayBuffer 或 ArrayBufferView。 发送过程非常简单,只需从控制器页面或接收器页面调用 connection.send(message) 即可。

// Controller page

function onSendMessageButtonClick() {
  presentationConnection.send('Hello!');
}

presentationConnection.addEventListener('message', function(event) {
  console.log('I just received ' + event.data + ' from the receiver.');
});

请查看 https://googlechrome.github.io/samples/presentation-api/ 中的示例,了解其工作原理。我相信您也会像我一样喜欢这款游戏。

示例和演示

请参阅我们在本文中使用的官方 Chrome 示例

此外,我还推荐使用互动式照片墙演示。此 Web 应用允许多个控制器在演示文稿显示屏上协作呈现照片幻灯片。https://github.com/GoogleChromeLabs/presentation-api-samples 可查看相关代码。

照片墙演示屏幕截图
照片 ,作者:José Luis Mieza/CC BY-NC-SA 2.0

还有一件事

Chrome 具有一个“投射”浏览器菜单,用户在访问网站时可以随时调用该菜单。如果您想控制此菜单的默认呈现方式,请将 navigator.presentation.defaultRequest 分配给之前创建的自定义 presentationRequest 对象。

// Make this presentation the default one when using the "Cast" browser menu.
navigator.presentation.defaultRequest = presentationRequest;

开发提示

如需检查接收器页面并对其进行调试,请转到内部 chrome://inspect 页面,选择“其他”,然后点击当前显示的网址旁边的“检查”链接。

检查演示文稿接收器页面
检查演示文稿接收器页面

您可能还需要查看内部 chrome://media-router-internals 页面,深入了解内部发现/可用性流程。

后续步骤

Chrome 66 及更高版本均支持 ChromeOS、Linux 和 Windows 平台。稍后将支持 Mac。

资源