2013-06-23 165 views
6

好的,我的問題很簡單。打開/關閉閃光燈

我設法打開閃光燈(並保持打開狀態)。

但是,我仍然不知道如何關閉它(大聲笑)。

這裏是我的代碼:

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 
    { 
     turnWhiteScreen(true); 
    } 

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

任何想法?


P.S.

  • 我想到的是,轉換.on s到.off小號可以工作,但事實並非如此。
  • 這已經過測試,在HTC 8S和的Lumia 820

回答

10

它看起來就像你不能獲取採集設備的兩倍(我不知道爲什麼),所以你應該把它保存在一個屬性:

protected AudioVideoCaptureDevice Device { get; set; } 

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

    try 
    { 
     if (this.Device == null) 
     { 
      // get the AudioViceoCaptureDevice 
      this.Device = 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)) 
     { 
      this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); 

      // set flash power to maxinum 
      this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, 
       AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max); 
     } 
     else 
     { 
      turnWhiteScreen(true); 
     } 

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

然後,將其關閉:

private void ButtonTurnOff_Click(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); 
    } 
} 
+0

嗯,這是驚人的 - 和這麼簡單! **有用**!非常感謝,哥們! ;-) –

0

試試這個

private static VideoTorchMode _videoTorchMode = VideoTorchMode.Off; 
private AudioVideoCaptureDevice _videoRecordingDevice; 

設備中存在檢查火炬。

private async void CheckTorch() { 
    if(AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) && 
     AudioVideoCaptureDevice.GetSupportedPropertyValues(CameraSensorLocation.Back, KnownCameraAudioVideoProperties.VideoTorchMode).ToList().Contains((UInt32)VideoTorchMode.On)) { 
    var temp = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back)[0]; 
    var resolution = new Windows.Foundation.Size(temp .Width, temp .Height); 
    _videoRecordingDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution); 
    } 
    else 
    MessageBox.Show("Your device does not support torch"); 
} 

要更改火炬狀態

private void SetTorchMode(){ 
    try { 
    if (BackgroundHandler.Instance.IsBackTorchExist) { 
     if (_videoTorchMode == VideoTorchMode.Off) { 
      _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); 
      _videoTorchMode = VideoTorchMode.On; 
     } 
     else { 
      _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off); 
      _videoTorchMode = VideoTorchMode.Off;          
     } 
     } 
    } 
    catch (Exception ex){ } 
}