簡體   English   中英

在Windows Phone Silverlight 8.1上使用MediaCapture打開手電筒

[英]Turn on torch with MediaCapture on Windows Phone Silverlight 8.1

我的問題很簡單,我無法使用Windows Phone 8.1中的MediaCapture API打開閃光燈。 (我成功使用了8.0 API)

我用兩個按鈕構建了一個非常簡單的項目,一個按鈕切換FlashControl,另一個按鈕切換TorchControl。

沒有崩潰,也不例外。 我的手機支持FlashControl和TorchControl。 我也逐步調試,一切看起來都不錯,單擊按鈕時值會更改。

這是我的代碼:

MediaCapture m_captureManager;

public MainPage()
{
  InitializeComponent();
}

private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
  DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
      .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
  if (deviceID != null) return deviceID;
  else throw new Exception(string.Format("Camera {0} doesn't exist", desiredCamera));
}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
  var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
  m_captureManager = new MediaCapture();

  await m_captureManager.InitializeAsync(new MediaCaptureInitializationSettings
  {
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
  });

}

private void button_ClickTorch(object sender, RoutedEventArgs e)
{
  var torch = m_captureManager.VideoDeviceController.TorchControl;
  if (torch.Supported)
  {
    if (torch.Enabled)
      torch.Enabled = false;
    else
      torch.Enabled = true;
  }
}

private void button_ClickFlash(object sender, RoutedEventArgs e)
{
  if (captureManager.VideoDeviceController.FlashControl.Supported)
  {
    if (captureManager.VideoDeviceController.FlashControl.Enabled)
      captureManager.VideoDeviceController.FlashControl.Enabled = false;
    else
      captureManager.VideoDeviceController.FlashControl.Enabled = true;
  }
}

這是一段簡單的代碼,我無法使其正常工作……我非常絕望,所以我嘗試使用一個中間對象進行切換,如您所見,沒有但沒有改變結果(這是一致的) 。

我終於找到了問題所在。 為了能夠使用相機服務,我們必須開始預覽。 由於Silverlight無法使用CaptureElement,因此必須將CustomPreviewSink與VideoBrush一起使用

這是怎么做的(來自Microsoft doc

private async void StartPreview()
{
previewSink = new Windows.Phone.Media.Capture.MediaCapturePreviewSink();

// List of supported video preview formats to be used by the default preview format selector.
var supportedVideoFormats = new List<string> { "nv12", "rgb32" };

// Find the supported preview format
var availableMediaStreamProperties = mediaCaptureManager.VideoDeviceController.GetAvailableMediaStreamProperties(
    Windows.Media.Capture.MediaStreamType.VideoPreview)
        .OfType<Windows.Media.MediaProperties.VideoEncodingProperties>()
        .Where(p => p != null 
            && !String.IsNullOrEmpty(p.Subtype) 
            && supportedVideoFormats.Contains(p.Subtype.ToLower()))
        .ToList();
var previewFormat = availableMediaStreamProperties.FirstOrDefault();

// Start Preview stream
await mediaCaptureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(
    Windows.Media.Capture.MediaStreamType.VideoPreview, previewFormat);
await mediaCaptureManager.StartPreviewToCustomSinkAsync(
    new Windows.Media.MediaProperties.MediaEncodingProfile { Video = previewFormat }, previewSink);


// Set the source of the VideoBrush used for your preview
Microsoft.Devices.CameraVideoBrushExtensions.SetSource(viewfinderBrush, previewSink);
}

將這段代碼添加到以前的代碼中,它將起作用。 重要的是在更改任何參數之前先啟動預覽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM