locked
[UWP] MediaCapture preview not showing anything RRS feed

  • Question

  • Hello everybody,

    I have a problem with all the apps I created in the past years: all of them share the functionality of previewing images from the webcam and saving them to the filesystem. My apps have two different approaches when taking pictures: the first one uses CameraCaptureUI and it works perfectly, the page calls the camera and I can take photos and videos. The second one uses MediaCapture instead, and despite the apps not giving me any error or exception, the preview is completely blank and CaptureElement is empty.

    I also tried to create a new app following the examples from MSDN (https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/simple-camera-preview-access), but nothing happens. This is the block of code from my test application:

    using Windows.Media.Capture;
    using Windows.ApplicationModel;
    using System.Threading.Tasks;
    using Windows.System.Display;
    using Windows.Graphics.Display;
    using Windows.UI.Xaml.Controls;
    using System;
    using Windows.UI.Core;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Input;
    
    namespace App
    {
        public sealed partial class MainPage : Page
        {
            MediaCapture _mediaCapture;
            bool _isPreviewing;
            DisplayRequest _displayRequest = new DisplayRequest();
    
    
            public MainPage()
            {
                InitializeComponent();
    
                Application.Current.Suspending += Application_Suspending;
            }
    
            private async Task StartPreviewAsync()
            {
                try
                {
                    _mediaCapture = new MediaCapture();
                    await _mediaCapture.InitializeAsync();
    
                    _displayRequest.RequestActive();
                    DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
                }
                catch (UnauthorizedAccessException)
                {
                    // This will be thrown if the user denied access to the camera in privacy settings
                    //ShowMessageToUser("The app was denied access to the camera");
                    return;
                }
    
                try
                {
                    PreviewControl.Source = _mediaCapture;
                    await _mediaCapture.StartPreviewAsync();
                    _isPreviewing = true;
                }
                catch (System.IO.FileLoadException)
                {
                    _mediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
                }
    
            }
    
            private async void _mediaCapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
            {
                if (args.Status == MediaCaptureDeviceExclusiveControlStatus.SharedReadOnlyAvailable)
                {
                    //ShowMessageToUser("The camera preview can't be displayed because another app has exclusive access");
                }
                else if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable && !_isPreviewing)
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        await StartPreviewAsync();
                    });
                }
            }
    
            private async Task CleanupCameraAsync()
            {
                if (_mediaCapture != null)
                {
                    if (_isPreviewing)
                    {
                        await _mediaCapture.StopPreviewAsync();
                    }
    
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        PreviewControl.Source = null;
                        if (_displayRequest != null)
                        {
                            _displayRequest.RequestRelease();
                        }
    
                        _mediaCapture.Dispose();
                        _mediaCapture = null;
                    });
                }
    
            }
    
            protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                
            }
    
            protected async override void OnNavigatedFrom(NavigationEventArgs e)
            {
                await CleanupCameraAsync();
            }
    
            private async void Application_Suspending(object sender, SuspendingEventArgs e)
            {
                // Handle global application events only if this page is active
                if (Frame.CurrentSourcePageType == typeof(MainPage))
                {
                    var deferral = e.SuspendingOperation.GetDeferral();
                    await CleanupCameraAsync();
                    deferral.Complete();
                }
            }
    
            private async void StartPreviewButton_Tapped(object sender, TappedRoutedEventArgs e)
            {
                await StartPreviewAsync();
            }
    
            private async void StopPreviewButton_Tapped(object sender, TappedRoutedEventArgs e)
            {
                await CleanupCameraAsync();
            }
        }
    }
    

    And this is my MainPage.xaml:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button
                Name="StartPreviewButton"
                Content="Avvia Preview"
                Tapped="StartPreviewButton_Tapped" />
            <Button
                Name="StopPreviewButton"
                Grid.Column="1"
                Content="Interrompi Preview"
                Tapped="StopPreviewButton_Tapped" />
            <CaptureElement
                Name="PreviewControl"
                Grid.Row="1"
                Grid.ColumnSpan="2"
                Stretch="Uniform" />
        </Grid>

    Appxmanifest has been updated correctly with microphone and webcam capabilities.

    Any suggestions?

    Regards,

    Matteo


    Tuesday, August 29, 2017 7:48 AM

All replies

  • Hi Matteo

    As you mentioned about “CaptureElement is empty”, please first check the camera access in the device's privacy settings to see if it is disabled for your app which using MediaCapture APIs. You could find it in “Settings -> Privacy -> Camera -> Your app”. Turn on the switch button and try again.

    There is also a sample about Camera that you could refer here: CameraStarterKit. Could you please try to use this sample and test again?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 30, 2017 7:31 AM
  • Hi Roy,
    Camera access is enabled for every application onmy device, so this is not a problem.
    I also downloaded the suggested sample and its behaviour is the same as in mine, a completely white screen as in the attached image.

    Regards,

    Matteo

    Wednesday, August 30, 2017 7:58 AM
  • @Matteo

    Could you please tell me your OS version?

    What device are you using for test?

    Is there any mobile device that you have to test about this issue?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 31, 2017 9:04 AM
  • OS version is Windows 10 Enterprise, Version 1703, build 15063.540.

    The device is my notebook, and for now I have no mobile device to test the issue.

    Thursday, August 31, 2017 3:09 PM
  • @Matteo

    I couldn't reproduce your problem in my device. It seems to be related with your environment. I suggest that you could find other's PC and test about this issue again to see if the issue still exists. 

    Besides, you could open a support ticket for help.

    Please kindly note that your support ticket will be free if it is Microsoft's issue.


    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, September 5, 2017 8:51 AM