2014-05-13 2 views
0

Je veux faire un ap qui allume le flash quand j'appuie sur le bouton On, et s'éteint quand j'appuie sur le bouton Off. Ceci est mon code:Comment allumer la lampe de poche dans une application sur Windows Phone 8

protected AudioVideoCaptureDevice Device { get; set; } 

    private async void Button_Click_TurnOn(object sender, RoutedEventArgs e) 
    { 
     var sensorLocation = CameraSensorLocation.Back; 

     try 
     { 
      // get the AudioViceoCaptureDevice 
      var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, 
       AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First()); 

      // 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 
       avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, 
        AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max); 
      } 
      else 
      { 
       //ShowWhiteScreenInsteadOfCameraTorch(); 
      } 

     } 
     catch (Exception ex) 
     { 
      // Flashlight isn't supported on this device, instead show a White Screen as the flash light 
      // ShowWhiteScreenInsteadOfCameraTorch(); 
     } 
    } 

    private void Button_Click_TurnOff(object sender, RoutedEventArgs e) 
    { 
     var sensorLocation = CameraSensorLocation.Back; 

     try 
     { 
      // turn flashlight on 
      var supportedCameraModes = AudioVideoCaptureDevice 
       .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode); 
      if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off)) 
      { 
       this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off); 
      } 
      else 
      { 
       //turnWhiteScreen(false); 
      } 
     } 
     catch (Exception ex) 
     { 
      // Flashlight isn't supported on this device, instead show a White Screen as the flash light 
      //turnWhiteScreen(false); 
     } 
    } 

Je l'ai copié d'une autre question à stackoverflow, mais je ne sais pas pourquoi ce code ne fonctionne pas pour moi. Testée sur Lumia 820.

S'il vous plaît aidez-moi, je vous remercie beaucoup :)

+0

double possible [Activer Flash On/Off] (http://stackoverflow.com/questions/17259293/turn- flash-on-off) – Howli

Répondre

0
async private void FlashlightOn_Click(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); 

      } 

     private void FlashlightOff_Click(object sender, RoutedEventAgrs e) 
     { 
      // turn flashlight off 
      var sensorLocation = CameraSensorLocation.Back; 
      FlashOn(sensorLocation, VideoTorchMode.Off); 
     } 


    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; 
    } 
Questions connexes