Using MediaCapturePreviewSink to preview video in a Windows Phone Silverlight 8.1 app

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

In Windows Phone 8.1, the Windows.Media.Capture APIs that previously were supported only for Windows apps are supported for Windows Phone apps, including Windows Phone Silverlight 8.1 apps. For information about using these APIs, see Capturing or rendering audio, video, and images. Because Silverlight 8.1 apps use a different XAML stack than Windows Phone Store apps, Silverlight 8.1 apps can use a new class, MediaCapturePreviewSink, to preview video from the capture device. This topic shows you a simple implementation of this scenario.

Using MediaCapturePreviewSink

This example method shows you how to use the MediaCapturePreviewSink object. This method performs the following tasks.

  1. Creates and initializes a new MediaCapture object and creates the MediaCapturePreviewSink object.

  2. Calls GetAvailableMediaStreamProperties(MediaStreamType) to retrieve a list of formats supported for video preview, and selects the first supported format.

  3. Calls the SetMediaStreamPropertiesAsync(MediaStreamType, IMediaEncodingProperties) method of the VideoDeviceController to set the format obtained in the previous step.

  4. Calls StartPreviewToCustomSinkAsync to begin preview streaming.

  5. Calls SetSource(VideoBrush, Camera) to hook up the preview sink to the video brush for the viewfinder.

private async void StartPreview()

{
    // Initialize the MediaCapture and MediaCapturePreviewSink objects
    mediaCaptureManager = new Windows.Media.Capture.MediaCapture();
    await mediaCaptureManager.InitializeAsync();
    previewSink = new Windows.Phone.Media.Capture.MediaCapturePreviewSink();

    // List of supported video preview formats to be used by the default preview format selector.
    var supportedVideoFormats = new List<string> { "nv12", "rgb32" };

    // Find the supported preview format
    var availableMediaStreamProperties =
        mediaCaptureManager.VideoDeviceController.GetAvailableMediaStreamProperties(
        Windows.Media.Capture.MediaStreamType.VideoPreview)
            .OfType<Windows.Media.MediaProperties.VideoEncodingProperties>()
            .Where(p => p != null 
                && !String.IsNullOrEmpty(p.Subtype) 
                && supportedVideoFormats.Contains(p.Subtype.ToLower()))
            .ToList();
    var previewFormat = availableMediaStreamProperties.FirstOrDefault();

    // Start Preview stream
    await mediaCaptureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(
        Windows.Media.Capture.MediaStreamType.VideoPreview, previewFormat);
    await mediaCaptureManager.StartPreviewToCustomSinkAsync(
        new Windows.Media.MediaProperties.MediaEncodingProfile { Video = previewFormat }, previewSink);


    // Set the source of the VideoBrush used for your preview
    Microsoft.Devices.CameraVideoBrushExtensions.SetSource(viewfinderBrush, previewSink);
}

Warning

The SetSource(VideoBrush, Camera) method is an extension method. This means that if you try to call it from an instance of a VideoBrush directly, it will not be resolved by the compiler unless you include the Microsoft.Devices namespace in your file.

To call the method like this:

viewfinderBrush.SetSource(previewSink);

You must include this directive:

using Microsoft.Devices;