在应用挂起后恢复相机资源



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

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

接口文档

上面写着:

您应该清理 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();
    }
}

在 Windows Phone 上,清理处理程序中的 MediaCapture 资源 ,并在 正在恢复事件。

所以我在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

有关使用 Visual Studio 进行调试的说明:Visual Studio 可防止 Windows 挂起附加到调试器的应用。这是为了允许用户在应用运行时查看 Visual Studio 调试 UI。调试应用时,可以使用 Visual Studio 向其发送挂起事件。确保显示"调试位置"工具栏,然后单击"挂起"图标。

最新更新