Recently , I was trying out an app to quickly enable the flash to build a torch application . Well , the developers can easily switch on the flash or switch off the flash from their App.
The AudioVideoCaptureDevice class can be used in Windows Phone 8 to turn on or turn off the flash from the device.
You can try using the VideoTorchMode of the camera which specifies how the torch is used when capturing video.
How to Turn the Flash On or Off from Windows Phone 8 App ?
To turn on the Flash in Windows Phone 8 App , its a 3 step process.
1. Decide the CameraSensorLocation that you will be using . (Generally it is the Back Camera) .
CameraSensorLocation location = CameraSensorLocation.Back;
2. Open the AudioVideoCaptureDevice specifying the CameraSensorLocation.
AudioVideoCaptureDevice audioCaptureDevice = await AudioVideoCaptureDevice.OpenAsync(location, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(location).First());
3. Verify if the device supports the VideoTorchMode and set the VideoTorchMode property of the VideoTorchMode to On.
var supportedCameraModes = AudioVideoCaptureDevice .GetSupportedPropertyValues(location, KnownCameraAudioVideoProperties.VideoTorchMode); if ((audioCaptureDevice !=null) && (supportedCameraModes.ToList().Contains((UInt32)mode))) { audioCaptureDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); return true; }
Note that you should enable ID_CAP_ISV_CAMERA and ID_CAP_MICROPHONE capabilities should be enabled in the WMAppManifest.xml file or else you will receive an unauthorized exception.
You should also take care or handle the exceptions when the phone does not supports flash.
Below is a sample code snippet demonstrating to both turn on flash as well as tuen off flash in Windows Phone app.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using MobileOSGeekApps.Resources; using Microsoft.Phone.Tasks; using System.Collections.ObjectModel; using System.Threading.Tasks; using Windows.Devices.Geolocation; using System.Device.Location; using Windows.Networking.Connectivity; using Windows.Phone.Media.Devices; using System.Xml.Linq; using Windows.Phone.Media.Capture; namespace MobileOSGeekApps { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } AudioVideoCaptureDevice audioCaptureDevice ; async private void Button_Click_1(object sender, RoutedEventArgs e) { // turn flashlight on CameraSensorLocation location = CameraSensorLocation.Back; if (this.audioCaptureDevice == null) { audioCaptureDevice = await AudioVideoCaptureDevice.OpenAsync(location, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(location).First()); } FlashOn(location, VideoTorchMode.On); } public bool FlashOn(CameraSensorLocation location, VideoTorchMode mode) { // turn flashlight on/off var supportedCameraModes = AudioVideoCaptureDevice .GetSupportedPropertyValues(location, KnownCameraAudioVideoProperties.VideoTorchMode); if ((audioCaptureDevice !=null) && (supportedCameraModes.ToList().Contains((UInt32)mode))) { audioCaptureDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, mode); return true; } return false; } private void Button_Click_2(object sender, RoutedEventArgs e) { // turn flashlight off var sensorLocation = CameraSensorLocation.Back; FlashOn(sensorLocation, VideoTorchMode.Off); } } }
1 Comment
Hi Senthil Kumar,
Thanks for the post.
The above code will just switch on and off the flash but it will not switch on continuously like torch. Is there any way to do like that ? Thanks