管理游戏广播

本文向你演示如何管理 UWP 应用的游戏广播。 用户必须使用内置在 Windows 中的系统 UI 启动广播,但从 Windows 10 版本 1709 开始,应用可以启动系统广播 UI,而且当广播开始和停止时可以收到通知。

将适用于 UWP 的 Windows 桌面扩展添加到你的应用

位于 Windows.Media.AppBroadcasting 命名空间的管理应用广播的 API 不包括在通用 API 协定中。 若要访问 API,你必须按照以下步骤将适用于 UWP 的 Windows 桌面扩展的引用添加到你的应用。

  1. 在 Visual Studio 的解决方案资源管理器中,展开你的 UWP 项目并右键单击引用,然后选择添加引用...
  2. 展开通用 Windows 节点并选择扩展
  3. 在扩展列表中,选中与你的项目的目标版本匹配的适用于 UWP 的 Windows 桌面扩展条目旁边的复选框。 对于应用广播功能,版本必须为 1709 或更高版本。
  4. 单击 “确定”

启动系统 UI 以允许用户启动广播

有几种原因导致你的应用当前可能无法广播,包括当前设备不满足广播所需的硬件要求,或另一个应用当前正在广播。 在启动系统 UI 前,你可以检查你的应用当前是否能够广播。 先检查广播 API 在当前设备上是否可用。 在操作系统版本早于 Windows 10 版本 1709 的设备上,这些 API 不可用。 也可以不检查具体的操作系统版本,改用 ApiInformation.IsApiContractPresent 方法查询 Windows.Media.AppBroadcasting.AppBroadcastingContract 版本 1.0。 如果此协定存在,则广播 API 在该设备上可用。

接下来通过在一次只有一位用户登录的电脑上调用工厂方法 GetDefault 获取 AppBroadcastingUI 类的实例。 在可以有多个用户登录的 Xbox 上,改为调用 GetForUser。 然后调用 GetStatus 获取应用的广播状态。

AppBroadcastingStatus 类的 CanStartBroadcast 属性向你指示应用当前是否可以开始广播。 如果不能,你可以检查详细信息属性以确定不可进行广播的原因。 根据具体原因,你可能要向用户显示状态或显示启用广播的说明。

// Verify that the edition supports the AppBroadcasting APIs
if (!Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent(
    "Windows.Media.AppBroadcasting.AppBroadcastingContract", 1, 0))
{
    return;
}

// On PC, call GetDefault because there is a single user signed in.
// On Xbox, call GetForUser instead because multiple users can be signed in.
AppBroadcastingUI^ broadcastingUI = AppBroadcastingUI::GetDefault();
AppBroadcastingStatus^ broadcastingStatus = broadcastingUI->GetStatus();

if (!broadcastingStatus->CanStartBroadcast)
{
    AppBroadcastingStatusDetails^ details = broadcastingStatus->Details;

    if (details->IsAnyAppBroadcasting)
    {
        UpdateStatusText("Another app is currently broadcasting.");
        return;
    }

    if (details->IsCaptureResourceUnavailable)
    {
        UpdateStatusText("The capture resource is currently unavailable.");
        return;
    }

    if (details->IsGameStreamInProgress)
    {
        UpdateStatusText("A game stream is currently in progress.");
        return;
    }

    if (details->IsGpuConstrained)
    {
        // Typically, this means that the GPU software does not include an H264 encoder
        UpdateStatusText("The GPU does not support broadcasting.");
        return;
    }

    // Broadcasting can only be started when the application's window is the active window.
    if (details->IsAppInactive)
    {
        UpdateStatusText("The app window to be broadcast is not active.");
        return;
    }

    if (details->IsBlockedForApp)
    {
        UpdateStatusText("Broadcasting is blocked for this app.");
        return;
    }

    if (details->IsDisabledByUser)
    {
        UpdateStatusText("The user has disabled GameBar in Windows Settings.");
        return;
    }

    if (details->IsDisabledBySystem)
    {
        UpdateStatusText("Broadcasting is disabled by the system.");
        return;
    }
    return;
}

通过调用 ShowBroadcastUI 请求系统显示应用广播 UI。

注意

ShowBroadcastUI 方法表示可能失败的请求,具体取决于系统的当前状态。 你的应用在调用此方法后不应假设已经开始广播。 使用在开始或停止广播时要通知的 IsCurrentAppBroadcastingChanged 事件。

broadcastingUI->ShowBroadcastUI();

当广播开始和停止时收到通知

通过初始化 AppBroadcastingMonitor 类的实例和注册 IsCurrentAppBroadcastingChanged 处理程序的方法进行注册以在用户使用系统 UI 开始或停止广播你的应用时收到通知。 如上文所述,在尝试使用广播 API 前,请务必在某个时候使用 ApiInformation.IsApiContractPresent 验证这些 API 在设备上存在。

if (Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent(
    "Windows.Media.AppBroadcasting.AppBroadcastingContract", 1, 0))
{
    m_appBroadcastMonitor = ref new AppBroadcastingMonitor();

    m_appBroadcastMonitor->IsCurrentAppBroadcastingChanged +=
        ref new TypedEventHandler<AppBroadcastingMonitor^, Platform::Object^>(this, &App::OnIsCurrentAppBroadcastingChanged);
}

IsCurrentAppBroadcastingChanged 事件的处理程序中,你可能想要更新应用的 UI 以反映当前广播状态。

void App::OnIsCurrentAppBroadcastingChanged(AppBroadcastingMonitor^ sender, Platform::Object^ args)
{
    if (sender->IsCurrentAppBroadcasting)
    {
        UpdateStatusText("App started broadcasting.");
    }
    else
    {
        UpdateStatusText("App stopped broadcasting.");
    }
}