我想做一个简单的应用程序,让我可以检查预览的每一帧的一些参数,但我在运行和停止预览时卡住了。
/// <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;
}
}
}在这种形式下,它可以完美地工作。
如果我取消对这些预览行的注释,它会起作用,但只有一次。
如果我按三次按钮: on、off和on,我在启用TorchControl时出现异常。
System.Exception: Pulsometr3.MainPage.d__d.MoveNext()的HRESULT异常: 0xE801000D ( Windows.Media.Devices.TorchControl.put_Enabled(Boolean值)
HRESULT会有所不同。
更奇怪的是,它有时会冻结手机(就像3次中的2次),我需要按住Power + Volume Down。
我试着用STAThread装饰所有的方法,但是没有用(http://technet.microsoft.com/en-ca/br226599)。
更有趣的是,当我使用F10按住debbuger的操作来跳过行的时候,我可以尽可能多次地切换预览。它是werid,因为调试器保存所有线程,对吗?所以理论上是没有区别的?
此外,手机有时会在部署时冻结...这实在是太烦人了。
有什么想法吗?
发布于 2015-01-05 23:37:56
我完全被this...for迷住了,原因是微软不太关心它的WP8的继任者操作系统,这让我真的很伤心。但也是半年前的夏天,我试过了,也许你可以尝试一下谷歌一下应用程序的同意书,也可以仔细检查你的应用程序清单,如果你有前置/后置摄像头和网络摄像头:)除此之外,如果它不起作用,那么倒霉的是,你应该坚持使用wp 8.0版本,它在wp 8.1上完全一样,所以不用担心:)还有其他库,比如facebook stuff或parse.com在wp 8.1 C#上不起作用:)
发布于 2016-04-09 04:55:24
我认为您的问题是启用了页面缓存。尝试在您的代码this.NavigationCacheMode = NavigationCacheMode.Required;中删除此行
发布于 2017-07-08 07:03:15
如果我理解正确的话,这个按钮有一个处理程序StartMeasure,它是一个异步方法,等待Start/StopPreviewAsync()。问题可能是,如果您多次单击按钮,一个操作可能仍在等待(正在进行),而另一个操作也被调用,这可能会导致一些问题,因为它将尝试同时启动和停止预览,这可能会导致一些竞争条件。您可以通过添加锁来管理对捕获管理器的访问来检查这一点,以便对其进行测试。此外,检查bool并在等待的操作后分配它肯定不是原子操作,因此这也可能导致竞争条件。
private object locker;
private async void StartMeasure(object sender, RoutedEventArgs e)
{
lock (locker)
{
if (_recording)
{
await _MediaCapture.StopPreviewAsync();
}
else
{
await _MediaCapture.StartPreviewAsync();
}
_recording = !_recording;
_MediaCapture.VideoDeviceController.TorchControl.Enabled = _recording;
}
}https://stackoverflow.com/questions/25514780
复制相似问题