在使用 MediaCapture
类 (Windows.Media.Capture
) 显示网络摄像头源的 Windows 8 应用程序 (C#) 中,我正在尝试重新启动预览当应用程序丢失然后重新聚焦时(例如,通过单击屏幕左上角的另一个应用程序,然后再次单击返回到我的应用程序)。
我现在尝试重新启动预览的方式是:
Application.Current.Resuming += (sender, o) => StartVideo(video);
Application.Current.Suspending += (sender, args) => StopVideo();
internal async void StartVideo(CaptureElement e)
{
try
{
this.stream = new MediaCapture();
await this.stream.InitializeAsync();
e.Source = this.stream;
await this.stream.StartPreviewAsync();
}
catch
{
new MessageDialog("Unable to start the video capture.").ShowAsync();
}
}
internal async void StopVideo()
{
try
{
await stream.StopPreviewAsync();
}
catch { }
}
但是,在我上面描述的示例中,Resuming
和 Suspending
事件似乎没有触发。这不是“暂停”应用程序吗?如果是这样,它是什么/我应该注意哪些事件?
或者,我是否应该使用 this.stream.StartRecord...
方法之一,而不是使用长时间运行的“预览”来显示网络摄像头?
编辑:如果我使用 Visual Studio 的暂停/恢复按钮(在“调试位置”工具栏上)手动触发事件,该功能会按预期运行(视频会在应用恢复时重新启动)。
最佳答案
我看到了一些错误:
- 你应该避免
async void
;对除事件处理程序之外的所有方法使用async Task
。 - 对于“命令”事件,例如
Suspending
,如果您有async
事件处理程序,请使用args.SuspendingOperation.GetDeferral
提供的延迟.
关于c# - 当应用重新获得焦点时重新启动 MediaCapture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14552301/