【问题标题】:Resuming Camera Resources after App Suspending应用挂起后恢复相机资源
【发布时间】:2015-04-20 09:43:57
【问题描述】:

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

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

API Documentation

上面写着:

您应该在 Windows Phone 上的 Suspending 事件中清理媒体捕获资源。

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 在应用从挂起状态恢复到前台状态后永远不会启动。

如何恢复相机资源?

【问题讨论】:

    标签: c# windows-phone windows-phone-8.1


    【解决方案1】:

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

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

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

    【讨论】:

    • 天啊......我从来没有知道......非常感谢 :) 它现在可以工作了
    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多