Window Phone 8.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Always wanted to develop a Wp8 Torch Program, see a lot of other development basically has the following problems:

1, The lock screen flash off.

2, Flash non-stop flashing.

I want to develop a lock screen can also use the flashlight, is very difficult to find information that. Find the code are not satisfactory, and some need to use the camera, and some need to use the recorder, feeling is not very bright.

Finally, after groping, Ui interface as long as a button, you can achieve the flashlight on and off, and Chang Liang, gibberish did not say more, code:

The Ui interface:

A button on the Ok:

<Button Name="btnFlash" Content="Flash lamp" Height="200" Click="btnFlash_Click_1"/>

The background page code:

private bool Flag = false;//Whether or not open flash lights
AudioVideoCaptureDevice avDevice = null;
// Constructor
public MainPage()
{
InitializeComponent();
}

private async void btnFlash_Click_1(object sender, RoutedEventArgs e)//Don't forget the async keyword (because the following code which uses the await keyword)
{
if (Flag)
{
//Flash has been opened to release resources (flash)
if (avDevice != null)
{
avDevice.Dispose();
Flag = false;
}
}
else
{
var sensorLocation = CameraSensorLocation.Back;
avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
//Open flash lights
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
Flag = true;
}
}
}

There is a special need to pay attention to the local:

Don't forget the inside of WMAppManifest.xml plus the two functions:

<Capability Name="ID_CAP_ISV_CAMERA" />
<Capability Name="ID_CAP_MICROPHONE" />

Thus a perfect flashlight is complete, of course, you can make the contact surface appearance, for example make Tencent security inside the housekeeper flashlight.

Reprint please indicate the source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Glendon at December 15, 2013 - 6:20 PM