如何知道UWP的另一个应用程序当前正在使用该摄像头?

时间:2017-06-10 00:23:49

标签: windows camera uwp c++-cx

我正在尝试显示错误消息“无法设置摄像头;当前正在使用”当已有进程运行摄像头时。我有使用MediaCapture启动预览的代码,并且在没有使用相机的其他应用程序的情况下运行时工作正常。我确实得到了例外

0x40080201:WinRT发起错误(参数:0xC00D3704,0x00000049,0x10EFF1CC)

在我的日志中但是我的try catch块没有捕获错误。

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

1 个答案:

答案 0 :(得分:1)

如果另一个应用程序具有对捕获设备的独占控制权,

StartPreviewAsync方法将抛出 FileLoadException 。我们可以捕获此错误以提醒用户该相机当前正被另一个应用程序使用。可以在Use MediaCapture to start the preview stream找到一个简单的样本。

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

<强> MainPage.xaml中

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

<强> 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::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());
        }
    });
}

StartPreviewToCustomSinkAsync方法类似于StartPreviewAsync方法,也可以抛出 FileLoadException 。您应该能够以相同的方式处理它。有关详情,请参阅Handle changes in exclusive control