在 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)
我用 2 个按钮构建了一个非常简单的项目,一个用于切换 FlashControl,另一个用于切换 TorchControl。
没有崩溃,没有异常。我的 phone 支持 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,我们必须使用带有 VideoBrush
的 CustomPreviewSink
这是怎么做的(来自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);
}
将这段代码添加到之前的代码中,它就会起作用。重要的一点是在更改任何参数之前开始预览
我的问题很简单,我无法使用 windows phone 8.1 的 MediaCapture API 打开手电筒。 (我用 8.0 成功了API)
我用 2 个按钮构建了一个非常简单的项目,一个用于切换 FlashControl,另一个用于切换 TorchControl。
没有崩溃,没有异常。我的 phone 支持 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,我们必须使用带有 VideoBrush
的 CustomPreviewSink这是怎么做的(来自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);
}
将这段代码添加到之前的代码中,它就会起作用。重要的一点是在更改任何参数之前开始预览