正确清理MediaCapture资源

时间:2014-11-10 10:39:32

标签: c# xaml windows-phone-8.1

对于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。我发现的一切都是先进的。

1 个答案:

答案 0 :(得分:1)

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

  

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

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

回到你的问题 - App.xaml.cs VideoREc.xaml 之间的连接 - 查看所有变量(本例中的属性)MediaCapturePreviewElementIsRecordingIsPreviewing在课程App中定义 - 它们是为整个应用定义的。我怀疑 VideoRec.xaml 仅使用 App.xaml.cs 中定义的那些属性的引用。

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

相关问题