如何知道UWP的另一个应用程序当前正在使用该相机? [英] How to know that camera is currently being used by another application with UWP?

查看:69
本文介绍了如何知道UWP的另一个应用程序当前正在使用该相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当已经有运行相机的进程时,我试图显示错误消息无法设置相机;当前正在使用".我有使用MediaCapture启动预览的代码,当在没有其他应用程序使用相机的情况下运行时,它可以正常工作.我确实有例外

I am trying to show error message "Cannot setup camera; currently being using" when there is already a process running the camera. I have the code that starts the preview using the MediaCapture and it works fine when running without another application using camera. I do get the exception

0x40080201:WinRT源错误(参数:0xC00D3704、0x00000049、0x10EFF1CC)

0x40080201: WinRT originate error (parameters: 0xC00D3704, 0x00000049, 0x10EFF1CC)

在我的日志中,但我的try catch块未捕获该错误.

in my logs but my try catch block doesn't catch the error.

create_task(_mediaCapture->StartPreviewToCustomSinkAsync(encoding_profile, media_sink)).then([this, &hr](task<void>& info) {
    try {
         info.get();
    } catch (Exception^ e) {
        hr = e->HResult;
    }
}).wait();

推荐答案

StartPreviewAsync 方法将引发 FileLoadException .我们可以捕获此错误,以警告用户该相机当前正在被其他应用程序使用.可以在

StartPreviewAsync method will throw a FileLoadException if another app has exclusive control of the capture device. We can catch this error to alert the user that the camera is currently being used by another application. A simple sample can be found at Use MediaCapture to start the preview stream.

但是,这是C#示例,而.net中使用的是 FileLoadException .对于C ++/CX,我们可以检查HRESULT值,该值应为 0x80070020 .以下是C ++/CX版本中的简单示例.

However, this is a C# sample and FileLoadException is what used in .Net. For C++/CX, we can check the HRESULT value which should be 0x80070020. Following is the simple sample in C++/CX version.

MainPage.xaml

MainPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <CaptureElement Name="PreviewControl" Stretch="Uniform"/>
    <Button Click="Button_Click">Click</Button>
</Grid>

MainPage.xaml.h

MainPage.xaml.h

public ref class MainPage sealed
{
public:
    MainPage();

private:
    // Prevent the screen from sleeping while the camera is running
    Windows::System::Display::DisplayRequest^ _displayRequest;

    // MediaCapture
    Platform::Agile<Windows::Media::Capture::MediaCapture^> _mediaCapture;

    void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};

MainPage.xaml.cpp

MainPage.xaml.cpp

MainPage::MainPage()
    : _mediaCapture(nullptr)
    , _displayRequest(ref new Windows::System::Display::DisplayRequest())
{
    InitializeComponent();
}


void MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _mediaCapture = ref new Windows::Media::Capture::MediaCapture();
    create_task(_mediaCapture->InitializeAsync())
        .then([this](task<void> previousTask) {
        try
        {
            previousTask.get();
            _displayRequest->RequestActive();
            Windows::Graphics::Display::DisplayInformation::AutoRotationPreferences = Windows::Graphics::Display::DisplayOrientations::Landscape;
            PreviewControl->Source = _mediaCapture.Get();
            create_task(_mediaCapture->StartPreviewAsync())
                .then([this](task<void>& previousTask)
            {
                try
                {
                    previousTask.get();
                }
                catch (Exception^ exception)
                {
                    if (exception->HResult == 0x80070020)
                    {
                        auto messageDialog = ref new Windows::UI::Popups::MessageDialog("Cannot setup camera; currently being using.");
                        create_task(messageDialog->ShowAsync());
                    }
                }
            });
        }
        catch (AccessDeniedException^)
        {
            auto messageDialog = ref new Windows::UI::Popups::MessageDialog("The app was denied access to the camera.");
            create_task(messageDialog->ShowAsync());
        }
    });
}

方法 StartPreviewAsync 方法,并且可以还抛出 FileLoadException .您应该能够使用相同的方式来处理它.有关更多信息,请参阅

StartPreviewToCustomSinkAsync method is similar to StartPreviewAsync method and can also throw a FileLoadException. You should be able to handle it using the same way. For more info, please also see Handle changes in exclusive control.

这篇关于如何知道UWP的另一个应用程序当前正在使用该相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆