在我的 Windows Store HTML5 应用程序中启用背景音频

Enabling background audio in my Windows Store HTML5 app

我有一个 HTML5 渐进式 Web 应用程序 (PWA) I've packaged into the Window Store thanks to PWABuilder。这是一款类似 Pandora 的音乐应用程序,可通过 HTML5 音频标签播放音频。

该应用程序在 Windows10 上运行良好,直到 我最小化该应用程序,然后音频停止播放。我希望我的应用程序即使在最小化时也能继续播放音频。

我的第一个想法是我需要在我的应用程序清单中声明适当的功能。所以,在 my appxmanifest.xml 中,我添加了:

<Capabilities>   
   <uap3:Capability Name="backgroundMediaPlayback" />
</Capabilities>

但即使有 backgroundMediaPlayback 功能,它也不起作用;最小化应用程序会停止音频。

查看 Universal Windows audio sample app,它说这应该有效:

"Adding the backgroundMediaPlayback capability enables all media playback APIs become background enabled. That means you can use any platform audio APIs, such as MediaPlayer, AudioGraph, XAudio2, and the HTML Audio tag."

如果我没看错,这意味着只需添加 backgroundMediaPlayback 功能就可以使我的 HTML5 音频标签在后台运行。 我还遗漏了什么吗?

这听起来像是应用程序正在暂停,而不是使用声明 backgroundMediaPlayback 功能 (MS blog here) 的应用程序可用的 Activity Sponsored Execution 模式。

检查您的应用程序在最小化时是否处于暂停状态。 您能让示例正常工作吗?

更新 1

看起来你的MinSDK和Target SDK设置为10240(Windows10的第一个版本)。将值更新为以下内容:

<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.14393.0" MaxVersionTested="10.0.16299.0"/>

更新 2

除了SDK版本,还需要添加SystemMediaTransportControls. See the 2nd bullet point in under the "Requirements for Background Audio" paragraph in this documentation article.

资源

我找到了将这些拼凑在一起的不同文档文章。