Search code examples
c#xamlwinrt-xamlwebcam

MediaCapture preview doesn't work


This is odd. It seems like the code to get a simple video stream and view it on a XAML form should be very simple indeed.

Suffice it to say, I have the following, very simple, View:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Border BorderBrush="White" BorderThickness="1" Width="400" Height="400" Padding="2">
        <CaptureElement x:Name="captureElement" Loaded="captureElement_Loaded" Stretch="Uniform"/>
    </Border>
    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Orientation="Horizontal">
        <Button Content="preview start" Tapped="StartPrevTapped" x:Name="prevStart"/>
        <Button Content="preview stop" Tapped=StopPrevTapped" x:Name="prevStop" IsEnabled="False"/>
    </StackPanel>
</Grid>

Here are the 3 methods:

    MediaCapture mediaCaptureManager = new MediaCapture();
    private async void captureElement_Loaded(object sender, RoutedEventArgs e)
    {
        var cameraDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

        var camera = cameraDevices.First();

        await mediaCaptureManager.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = camera.Id });
    }

    private async void StartPrevTapped(object sender, TappedRoutedEventArgs e)
    {
        captureElement.Source = mediaCaptureManager;

        await mediaCaptureManager.StartPreviewAsync();
        prevStart.IsEnabled = false;
        prevStop.IsEnabled = true;
    }

    private async void StopPrevTapped(object sender, TappedRoutedEventArgs e)
    {
        await mediaCaptureManager.StopPreviewAsync();
        prevStart.IsEnabled = true;
        prevStop.IsEnabled = false;
    }

The code doesn't throw any exceptions, but the CaptureElement doesn't display anything. What could be wrong?

A few additional notes:

  • I'm writing the code on an online development machine which doesn't actually have a web camera. As such, I installed the free version of ManyCam. TestMyCam showed it working just fine.
  • Due to the same reasons as above, there's no audio recording device.
  • I've tried to use the code provided by this MSDN sample project. It also was unable to display any preview. In fact the project, without any modifications, failed to even start the preview, because it assumed there will be a microphone attached.

EDIT:

Strange things are happening. It seems the issues are caused by a strange configuration on the development machine I was provided, rather than the code itself.

  • await mediaCaptureManager.InitializeAsync, which previously caused no errors, now throws an exception with the message The RPC server is unavailable. No further information is provided. The message is crazy, since it's not related to media at all!

  • I've finally managed to get my hands on a laptop with a webcam and VS installed, and it worked fine. No clue how to tackle the issue going forward - I cannot continue development on this laptop and I could only use it for this test.


Solution

  • It turns out the issues are purely with the development machine provided. When testing code on my local machine it works perfectly fine.

    Come to think about it, this wouldn't be the first time this development machine threw a fit; for some reason the "tablet simulator" available in VS also fails to start up...