How to adjust camera or microphone settings (HTML)
[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]
You can adjust various settings on the camera or microphone, such as brightness, contrast, focus (on the camera) or volume (on the microphone). In the Windows.Media.Capture API, this is done by retrieving a Windows.Media.Devices.VideoDeviceController or Windows.Media.Devices.AudioDeviceController object and setting the properties of the object.
Prerequisites
- You should be familiar with JavaScript.
- The computer you are using has a camera.
Instructions
Step 1: Retrieve a Video or Audio Device Controller
The Windows.Media.Capture.MediaCapture object contains the VideoDeviceController and AudioDeviceController properties that allow you to retrieve a Windows.Media.Devices.VideoDeviceController or a Windows.Media.Devices.AudioDeviceController object to control the settings of a video or an audio device.
// Create the media capture object.
var oMediaCapture = new Windows.Media.Capture.MediaCapture();
oMediaCapture.initializeAsync();
// Retrieve a video device controller.
var videoDeviceController = oMediaCapture.videoDeviceController;
// Retrieve an audio device controller.
var audioDeviceController = oMediaCapture.audioDeviceController;
Step 2: Set the Properties of a Video Device Controller
The VideoDeviceController property returns a Windows.Media.Devices.VideoDeviceController object. The properties of this object such as brightness, contrast or focus each return a MediaDeviceControl object with a Capabilities property that returns a MediaDeviceControlCapabilities object. The MediaDeviceControlCapabilities object has properties and methods that allow you to determine if a property is supported for this camera, what the minimum and maximum values of the property are, and will allow you to get and set the property value.
The following example retrieves a MediaDeviceControlCapabilities object called brightnessCapabilities for the brightness setting of a video camera, and uses it to increase the brightness level.
// Retrieve the brightness capabilites of the video camera
var brightnessCapabilities = videoDeviceController.brightness.capabilities;
//
// Determine if the video camera supports adjustment of the brightness setting.
//
if (brightnessCapabilities.supported)
{
var brightness;
//
// Retrieve the current brightness value.
//
if (videoDeviceController.brightness.tryGetValue( brightness ))
{
//
// Get the minimum, maximum and step size for the brightness value.
//
var min = brightnessCapabilities.min;
var max = brightnessCapabilities.max;
var step = brightnessCapabilities.step;
//
// Increase the brightness value by one step as long as the new value is less than or equal to the maximum.
//
if( (brightness + step) <= max )
{
if( brightnessCapabilities.trySetValue( brightness + step ) )
{
// The brightness was successfully increased by one step.
}
else
{
// The brightness value couldn't be increased.
}
}
else
{
// The brightness value is greater than the maximum.
}
}
else
{
// The brightness value couldn't be retrieved.
}
}
else
{
// Setting the brightness value is not supported on this camera.
}
Step 3: Set the Properties of an Audio Device Controller
The AudioDeviceController property returns a Windows.Media.Devices.AudioDeviceController object. The properties of this object such as Muted, and VolumePercent can be used to directly adjust the microphone settings.
The following example shows how to use an AudioDeviceController object to mute/unmute the microphone and to increase the microphone volume.
// Mute the microphone.
audioDeviceController.muted = true;
// Un-mute the microphone.
audioDeviceController.muted = false;
// Get the current volume setting.
var currentVolume = audioDeviceController.volumePercent;
// Increase the volume by 10 percent, if possible.
if (currentVolume <= 90) {
audioDeviceController.volumePercent = (currentVolume + 10);
}
Step 4: Complete Example
The following example shows how to adjust camera and microphone settings when capturing video to a file. The entry point in this example is the StartMediaCaptureSession function. This function then calls the SetDevices function where the video camera brightness and microphone volume are adjusted before the capture session begins. For an example of how create the capture session, see Quickstart: Capturing video using the MediaCapture API.
//
// Initialize MediaCapture global object
//
var oMediaCapture;
function startMediaCaptureSession() {
oMediaCapture = new Windows.Media.Capture.MediaCapture();
oMediaCapture.initializeAsync().then (function (result) {
// Set the audio and video.
setDevices();
}, errorHandler);
}
function setDevices()
{
//
// Obtain Video and Audio device controllers.
//
var videoDeviceController = oMediaCapture.videoDeviceController;
var audioDeviceController = oMediaCapture.audioDeviceController;
//
// Adjust Video and Audio device settings.
//
//
// Increase the brightness value by one step as long as the new value is less than or equal to the maximum.
//
var brightness;
var brightnessCapabilities = videoDeviceController.brightness.capabilities;
brightness = videoDeviceController.brightness.tryGetValue();
if( (brightness.value + brightnessCapabilities.step) <= brightnessCapabilities.max )
{
if (videoDeviceController.brightness.trySetValue(brightness + brightnessCapabilities.step))
{
// The brightness value was successfully increased by one step.
}
else
{
// The brightness value could not be increased.
}
}
else
{
// The new brightness value would be greater than the maximum value.
}
//
// Increase the microphone volume by 10 percent if possible.
//
var increase = 10;
var currentVolume = audioDeviceController.volumePercent;
if (currentVolume + increase <= 100) {
audioDeviceController.volumePercent += increase;
}
}
Remarks
You can also launch a dialog for adjusting camera settings by calling Windows.Media.Capture.CameraOptionsUI.Show, and passing a MediaCapture object as a parameter.