1

For a Windows Phone 8.1 app I have to record a video.

I used this instructions and it works basically... http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868171.aspx

... but I do not get the cleaning up part within the 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();
}

I don't get how the connection between App.xaml.cs and VideoRec.xaml (whrere the preview element is) has to work. This is probably a very basic thing... I am very grateful for every hint or a link for a tutorial for beginners how to handle MediaCapture at all. Everything I have found is for advanced.

user3168511
  • 254
  • 1
  • 15

1 Answers1

1

I would say that proper cleaning MediaCapture is the most important part of takieng a photo/video also MSDN says:

Note On Windows Phone, music and media apps should clean up the MediaCapture object and associated resources in the Suspending event handler and recreate them in the Resuming event handler.

What happens when you don't clean your MediaCapture element? - in my case, when I've tried, my phone hung when I've run other application using MediaCapture (for example default photo app).

Going back to your question - connection between App.xaml.cs and VideoREc.xaml - look that all variables (properties in this case) MediaCapture, PreviewElement, IsRecording and IsPreviewing are defined in class App - they are defined for the whole app. I suspect that VideoRec.xaml uses only a reference of those properties defined in App.xaml.cs.

You should also be aware that Suspending/Resuming events are defined for the whole app and they are all fired when such an event occurs. When does it occur? - just after you navigate away from your app (only look out for Debug mode - it works little different when connected to computer). More about lifecycle events at MSDN. Those events are the best one to clean/restore MediaCapture resources.

Romasz
  • 29,662
  • 13
  • 79
  • 154