delilah
delilah

Reputation: 63

What should I do to access camera in a UWP application using MediaCapture?

I try to access camera in a UWP application but I have an error:

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
WinRT information: Access is denied.

The app was denied access to the camera
The thread 0x2c9c has exited with code 0 (0x0).
The program '[3352] CameraGetPreviewFrame.exe' has exited with code 1 (0x1).

Here is my code:

if (cameraDevice == null)
{
    Debug.WriteLine("No camera device found!");
    return;
}

// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();

// Register for a notification when something goes wrong
_mediaCapture.Failed += MediaCapture_Failed;

var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };

// Initialize MediaCapture
try
{
    await _mediaCapture.InitializeAsync(settings);
    _isInitialized = true;
}
catch (UnauthorizedAccessException)
{
    Debug.WriteLine("The app was denied access to the camera");
}

Could you explain to me why does the exception occur and how to solve the problem?

Upvotes: 2

Views: 920

Answers (1)

Vijay Nirmal
Vijay Nirmal

Reputation: 5837

You should set Microphone and Webcam Capabilities for your project. To do it you can follow the steps below

  1. Open Package.appxmanifest
  2. Go to Capabilities Tab
  3. Check Microphone and Webcam

Upvotes: 3

Related Questions