gpt4 book ai didi

c# - App挂起后恢复相机资源

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:39 26 4
gpt4 key购买 nike

我正在编写 Windows Phone 8.1 (WINPRT-XAML) 应用程序。

我正在使用 MediaCapture API 在应用中录制视频。

API Documentation

是这样写的:

You should cleanup media capture resources in the Suspending event on Windows Phone.

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();

//cleanup camera resources
await CleanupCaptureResources();

deferral.Complete();
}

public async Task CleanupCaptureResources()
{
if (IsRecording && MediaCapture != null)
{
await MediaCapture.StopRecordAsync();
IsRecording = false;
}
if (IsPreviewing && MediaCapture != null)
{
await MediaCapture.StopPreviewAsync();
IsPreviewing = false;
}

if (MediaCapture != null)
{
if (PreviewElement != null)
{
PreviewElement.Source = null;
}
MediaCapture.Dispose();
}
}

On Windows Phone, clean up your MediaCapture resources in the handler for the Suspending app lifetime event and recreate them in the Resuming event.

所以我在 App.cs 中添加了以下几行:

 public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
this.Resuming += this.App_Resuming;
}

void App_Resuming(object sender, object e)
{
//TODO: RESUME CAMERA PREVIEW and MediaCapture object here
}

但是 App_Resuming 在应用从暂停状态恢复到前台状态后永远不会启动。

如何恢复相机资源?

最佳答案

您确定您的测试正确吗?如果您正在调试应用程序,它们将不会暂停(因此它们无法恢复),但您可以从 Visual Studio 触发生命周期事件。

更多信息在这里:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465115.aspx

A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.

关于c# - App挂起后恢复相机资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com