App挂起后恢复相机资源

Resuming Camera Resources after App Suspending

我正在写一个Windows Phone 8.1 (WINPRT-XAML) App.

我正在使用 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.