我想制作一个简单的应用程序,可以让我检查每个预览帧的几个参数,但我在运行和停止预览时遇到了困难。
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
MediaCapture _MediaCapture;
bool _recording;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var rearCamera = devices[0];
if (devices.Count > 0)
{
rearCamera = devices.Single(currDev =>
currDev.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
);
}
_MediaCapture = new MediaCapture();
await _MediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { VideoDeviceId = rearCamera.Id });
// this is CaptureElement
xCapture.Source = _MediaCapture;
_recording = false;
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
if(_MediaCapture != null)
{
await _MediaCapture.StopPreviewAsync();
await _MediaCapture.StopRecordAsync();
_MediaCapture.Dispose();
_MediaCapture = null;
xCapture.Source = null;
}
base.OnNavigatedFrom(e);
}
// button click handler
private async void StartMeasure(object sender, RoutedEventArgs e)
{
if (_recording)
{
//await _MediaCapture.StopPreviewAsync();
_MediaCapture.VideoDeviceController.TorchControl.Enabled = false;
_recording = false;
}
else
{
//await _MediaCapture.StartPreviewAsync();
_MediaCapture.VideoDeviceController.TorchControl.Enabled = true;
_recording = true;
}
}
}
这个表单在这种形式下完美地运作。
如果我取消对那些预览行的注释,它可以工作,但仅限于一次。
如果我按按钮三次:打开、关闭和再次打开,我会在启用TorchControl的那一行遇到异常。
System.Exception: Exception from HRESULT: 0xE801000D at Windows.Media.Devices.TorchControl.put_Enabled(Boolean value) at Pulsometr3.MainPage.d__d.MoveNext()
HRESULT的值是不同的。
更奇怪的是,有时它会导致手机冻结(大约三次中有两次),我需要按住电源键+音量减小键才能解决。
我尝试在所有方法上添加[STAThread]修饰符,但没有帮助 (http://technet.microsoft.com/en-ca/br226599)。
更加有趣的是,当我使用F10调试器暂停操作以跳过某些代码行时,我可以随意切换预览。这很奇怪,因为调试器会暂停所有线程,对吧?所以理论上没有区别?
此外,手机有时会在部署时冻结...真烦人。
有什么想法吗?