视频录制设备被另一个沉浸式应用程序抢占

时间:2017-04-26 14:58:43

标签: xaml uwp windows-10 windows-10-universal

我是否打开相机拍照,但是如果用户不再想要捕捉某些东西,用户可以停止相机,所以我有一个关闭按钮,用于关闭相机,以便相机预览应该停止。

如果我打开相机,关闭,再次打开,我会在第二次点击关闭按钮后出现以下异常:

System.Runtime.InteropServices.COMException: 'The video recording device is preempted by another immersive application.

我不知道,预览相机应该如何停止,而不是UWP文档在这里说:https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/simple-camera-preview-access

停止相机预览的代码:

private async Task CleanupCameraAsync()
    {
        if (_mediaCapture != null)
        {
            if (_isPreviewing)
            {
                await _mediaCapture.StopPreviewAsync();
            }

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                CameraPreviewControl.Source = null;
                if (_displayRequest != null)
                {
                    _displayRequest.RequestRelease();
                }

                _mediaCapture.Dispose();
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

我尝试在我身边测试你的代码片段,它可以正常工作。我没有得到上述例外。我在模拟器build 15063上测试并构建了14393.由于你的代码片段不是完整的代码,所以我创建了一个最小的测试项目,如下所示,这对我来说很有用。如果您的项目出现问题,您可以测试它并与您的代码进行比较。

XAML

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="30"> 
   <CaptureElement Name="PreviewControl" Stretch="Uniform"/>
   <Button x:Name="btnpreview" Click="btnpreview_Click" Content="preview test"></Button>
   <Button x:Name="btnstop" Click="btnstop_Click" Content="stop"></Button>
</StackPanel>

背后的代码

private DeviceInformation _cameraDevice;
private MediaCapture _mediaCapture;
private InMemoryRandomAccessStream _ras;
private LowLagMediaRecording _recording;
private CameraRotationHelper _rotationHelper;
private readonly DisplayRequest _displayRequest = new DisplayRequest();
private bool _isPreviewing; 

public MainPage()
{
    this.InitializeComponent();
}
private async void btnstop_Click(object sender, RoutedEventArgs e)
{ 
    if (_mediaCapture != null)
    {
        if (_isPreviewing)
        {
            await _mediaCapture.StopPreviewAsync();
            _isPreviewing = false;
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                PreviewControl.Source = null;
                if (_displayRequest != null)
                {
                    _displayRequest.RequestRelease();
                }
            _mediaCapture.Dispose();
            });
        }
    }
} 
private async void btnpreview_Click(object sender, RoutedEventArgs e)
{
    try
    {
        _mediaCapture = new MediaCapture();
        var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
        var desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
        _cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault();
        _rotationHelper = new CameraRotationHelper(_cameraDevice.EnclosureLocation);
        _mediaCapture.Failed += MediaCapture_Failed;
        var settings = new MediaCaptureInitializationSettings { VideoDeviceId = _cameraDevice.Id };
        await _mediaCapture.InitializeAsync(settings);   
        PreviewControl.Source = _mediaCapture;
        _displayRequest.RequestActive();
        await _mediaCapture.StartPreviewAsync();
        _isPreviewing = true; 
    }
    catch (UnauthorizedAccessException)
    {
        // This will be thrown if the user denied access to the camera in privacy settings
        System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
    }
}

如果您仍有问题,请提供最小的转载项目和测试环境信息。更多详情请参阅official sample