Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Using the camera light in Windows Phone 7, 8 and 8.1

From Wiki
Jump to: navigation, search

Using the camera light as a torch is a common use case for smartphones. Windows Phone presents an evolution of APIs supporting this functionality.

WP Metro Icon Multimedia.png
Article Metadata
Created: influencer (10 May 2014)
Last edited: influencer (16 May 2014)



Using the camera light as a torch is not easy in Windows Phone. Each version has different APIs that only works on one OS version. Each version has different requirements regarding app capabilities. This article presents a wrap-up for the different versions of the OS.

Windows Phone 7

In Windows Phone 7 there is an API that's exposed in an undocumented assembly, Microsoft.Phone.Media.Extended.dll. It can be called by using reflection. Its drawback is that this assembly is not available on Windows Phone 8 or later.


While we are at the capabilities: there's a catch. You can define the capabilities in the manifest and your app will run fine - at least during debugging. When you prepare your release build and upload it into the store, you might get a surprise when the first users give fatal reviews - the app crashes! What's the reason? The store certification tool checks your app for the necessary capabilities and removes unnecessary capabilities. So your important caps get removed! And what can you do about it? Persuade the store you really need the caps by using another API that needs the same capabilities. Here's the code you can use to do just that:

// Capability detection workarounds
Microsoft.Xna.Framework.Audio.Microphone mic = Microsoft.Xna.Framework.Audio.Microphone.Default;
Microsoft.Xna.Framework.Media.MediaLibrary lib = new Microsoft.Xna.Framework.Media.MediaLibrary();

Place it into the XXXPage.OnNavigatedFrom handler (or somewhere else).

Here's the core code to switch on the camera light:

// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
// Use standard camera on back of device.
_videoCamera = new VideoCamera();
// Event is fired when the video camera object has been initialized.
_videoCamera.Initialized += VideoCamera_Initialized;
// Add the photo camera to the video source
_videoCameraVisualizer = new VideoCameraVisualizer();
catch (Exception)
// Flashlight isn't supported on this device, instead show a White Screen as the flash light

This code is borrowed from Windows Phone Flashlight: Getting started and uses the VideoCamera and VideoCameraVisualizer classes defined there.

Windows Phone 8

On Windows Phone 8 the above API can't be used as the assembly is not available any more. But we have a new API set in Windows.Phone.Media.Capture.

Necessary capabilities: ID_CAP_ISV_CAMERA, ID_CAP_MICROPHONE

The WP8 code is much shorter (remember the supporting classes) and is a big improvement over WP7.

var sensorLocation = CameraSensorLocation.Back;
// get the AudioViceoCaptureDevice
var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
// turn flashlight on
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
// set flash power to maxinum
AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
catch (Exception)
// Flashlight isn't supported on this device, instead show a White Screen as the flash light

Windows Phone 8.1

Windows Phone 8.1 is the first version with a dedicated API for controlling the camera light. This API stems from Windows 8.1 but is usable in Windows Phone 8.1 projects and in Windows Phone Silverlight 8.1 projects.

Necessary capabilities: Webcam

var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
if (tc.PowerSupported)
tc.PowerPercent = 100;
tc.Enabled = true;

Note.pngNote: TorchControl.Supported returns false on most phones in WP8.1 developer preview. It is expected to be fixed by a firmware update by the time WP 8.1 is released. Tested Phones at the time of writing: Lumia 620, 822, 1020: not working, Lumia 1520: working.


The example of TorchControl shows how the APIs have improved in WP 8.1. While simplifying the classes the capability requirements are reduced.

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 16 May 2014, at 20:56.
672 page views in the last 30 days.