【问题标题】:Cleaning up MediaCapture resources properly正确清理 MediaCapture 资源
【发布时间】:2014-11-10 10:39:32
【问题描述】:

对于 Windows Phone 8.1 应用,我必须录制视频。

我使用了这个说明,它基本上可以工作...... http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868171.aspx

...但我没有得到 App.xaml.cs 中的清理部分

public MediaCapture MediaCapture { get; set; }
public CaptureElement PreviewElement { get; set; }
public bool IsRecording { get; set; }
public bool IsPreviewing { get; set; }

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();
    }
}

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

    //cleanup camera resources
    await CleanupCaptureResources();

    deferral.Complete();
}

我不明白 App.xaml.cs 和 VideoRec.xaml(预览元素所在的位置)之间的连接必须如何工作。 这可能是一件非常基本的事情......我非常感谢初学者如何处理 MediaCapture 的每个提示或教程链接。我找到的所有内容都是针对高级的。

【问题讨论】:

    标签: c# xaml windows-phone-8.1


    【解决方案1】:

    我会说正确清洁 MediaCapture 是拍摄照片/视频最重要的部分,MSDN 说:

    注意在 Windows Phone 上,音乐和媒体应用应在 Suspending 事件处理程序中清理 MediaCapture 对象和相关资源,并在 Resuming 事件处理程序中重新创建它们。

    如果您不清理 MediaCapture 元素会发生什么? - 在我的情况下,当我尝试使用 MediaCapture 运行其他应用程序(例如默认照片应用程序)时,我的手机挂起。

    回到你的问题 - App.xaml.csVideoREc.xaml 之间的连接 - 看看所有变量(在这种情况下为属性)MediaCapture,@ 987654324@、IsRecordingIsPreviewing 在类 App 中定义 - 它们是为整个应用程序定义的。我怀疑 VideoRec.xaml 仅使用 App.xaml.cs 中定义的那些属性的引用。

    您还应该知道,Suspending/Resuming 事件是为整个应用程序定义的,并且在发生此类事件时都会触发它们。什么时候发生? - 就在您离开您的应用程序之后(仅注意 Debug 模式 - 连接到计算机时它的工作方式略有不同)。更多关于lifecycle events at MSDN。这些事件是清理/恢复 MediaCapture 资源的最佳事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2010-11-10
      • 2021-12-29
      • 1970-01-01
      相关资源
      最近更新 更多