是否可以在iOS上的HTML5应用程序中使用JavaScript中的Airplay显示屏(如电视

bhmjp9jg  于 2023-03-05  发布在  iOS
关注(0)|答案(1)|浏览(113)

我想用javascript创建一个HTML5应用程序,它可以在iOS设备上的Safari中运行,但也可以将Airplay用作第二个屏幕或扩展屏幕(而不是镜像)。我的想法是在设备上有一个UI,在Airplay设备上有一个单独的UI。我知道这可以在原生应用程序中完成,但如果我也可以在浏览器中完成,这可能会很有趣。

wwodge7n

wwodge7n1#

我不知道您使用什么iOS设备进行测试,但您可以尝试在iOS上使用Safari的WebKit API。

if (window.WebKitPlaybackTargetAvailabilityEvent) {
  // Airplay is available
  var video = document.getElementById('video');
  video.addEventListener('webkitplaybacktargetavailabilitychanged', function(event) {
    if (event.availability === 'available') {
      // Airplay receiver is available
      var receiverName = event.target.getAttribute('webkitCurrentPlaybackTargetName');
      var picker = new WebKitPlaybackTargetPicker();
      picker.showPicker(function (receiver) {
        // Airplay receiver has been selected
        var receiverUrl = receiver.url;
        var externalWindow = window.open(receiverUrl);
        externalWindow.focus();
      }, function () {
        console.log('Airplay receiver selection failed');
      }, video);
    }
  });
}

但您必须知道,并非所有Airplay接收器都支持显示HTML内容,因此您可能需要测试不同的接收器,以确定哪些接收器可以与您的应用配合使用。
Safari Webkit的HTML内容示例

// Function to display HTML content on an external AirPlay-enabled display

function displayOnAirPlay(htmlContent) {

  // Call webkitShowPlaybackTargetPicker to allow user to select AirPlay device

  webkitShowPlaybackTargetPicker(function(target) {
    if (target) {
     
 // If a target device was selected, create a new window and load the HTML content

      var externalWindow = target.createRemoteWebView();
      externalWindow.loadHTMLString(htmlContent, { baseURL: null });
      externalWindow.show();
    }
  });
}

// usage: display the contents of a div with ID "content" on an external display

var content = document.getElementById("content").innerHTML;
displayOnAirPlay(content);

这是一个HTML内容的工作示例,在所选设备中有一个基本按钮和基本显示内容。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AirPlay Example</title>
</head>
<body>
  <h1>AirPlay Example</h1>
  <div id="content">
    <p>This is some HTML content that will be displayed on the external AirPlay-enabled display.</p>
  </div>
  <button id="displayButton">Display on AirPlay</button>

  <script>
    // Get a reference to the display button and the content div
    const displayButton = document.getElementById('displayButton');
    const contentDiv = document.getElementById('content');

    // Add an event listener to the display button
    displayButton.addEventListener('click', async () => {
      try {
        // Request access to the ExternalOutputContext API
        const context = await navigator.externalOutputContext.request();

        // Get the list of available external displays
        const displays = await context.getDisplays();

        // Prompt the user to select an AirPlay-enabled display
        const selectedDisplay = await context.showDisplayPicker(displays);

        // If a display was selected, create a new window and load the HTML content
        if (selectedDisplay) {
          const externalWindow = await selectedDisplay.createWindow();
          externalWindow.document.body.innerHTML = contentDiv.innerHTML;
          externalWindow.show();
        }
      } catch (error) {
        console.error(error);
      }
    });
  </script>
</body>
</html>

相关问题