我需要在UWP中使用录音 但是如果应用程序最小化则无法录音。
有没有办法在不使用受限能力的情况下做到这一点?
使用扩展执行会话。它将允许您的 uwp 应用程序在最小化时录制音频。以下是链接 https://learn.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution 了解详细信息。我已经尝试过了,它对我来说效果很好。 添加 EnteredBackground 和 LeavingBackground 事件:
this.EnteredBackground += AppEnteredBackground;
this.LeavingBackground += AppLeavingBackground;
当检测到事件时,调用BeginExtendedExecution,当允许会话时,调用捕获音频函数。
private async void BeginExtendedExecution()
{
ClearExtendedExecution();
var newSession = new ExtendedExecutionSession();
newSession.Reason = ExtendedExecutionReason.Unspecified;
newSession.Description = "recording audio";
newSession.Revoked += SessionRevoked;
ExtendedExecutionResult result = await newSession.RequestExtensionAsync();
switch (result)
{
case ExtendedExecutionResult.Allowed:
session = newSession;
RecordingAudio();
break;
default:
case ExtendedExecutionResult.Denied:
newSession.Dispose();
break;
}
}
在后台录制音频是一项受限制的功能。
添加受限能力的方法如下:
package.appxmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="... rescap">
...
<Capabilities>
<rescap:Capability Name="backgroundMediaRecording"/>
</Capabilities>
</Package>
添加此功能后,应用程序可以在最小化时继续录制音频。
但是,应该注意的是,这是一种有限的能力。如果您想在 Microsoft Store 上架,则需要提供额外的说明来描述使用此功能的原因。