VideoDeviceController.TryAcquireExclusiveControl 方法

定义

请求对具有指定设备 ID 的相机进行独占控制。

public:
 virtual bool TryAcquireExclusiveControl(Platform::String ^ deviceId, MediaCaptureDeviceExclusiveControlReleaseMode mode) = TryAcquireExclusiveControl;
bool TryAcquireExclusiveControl(winrt::hstring const& deviceId, MediaCaptureDeviceExclusiveControlReleaseMode const& mode);
public bool TryAcquireExclusiveControl(string deviceId, MediaCaptureDeviceExclusiveControlReleaseMode mode);
function tryAcquireExclusiveControl(deviceId, mode)
Public Function TryAcquireExclusiveControl (deviceId As String, mode As MediaCaptureDeviceExclusiveControlReleaseMode) As Boolean

参数

deviceId
String

Platform::String

winrt::hstring

为其请求独占控制的相机的设备 ID。 可以使用 DeviceInformation 类获取设备 ID。

mode
MediaCaptureDeviceExclusiveControlReleaseMode

MediaCaptureDeviceExclusiveControlReleaseMode 枚举中的值,指定释放独占控件的条件。

返回

Boolean

bool

如果获得了相机的独占控制,则为 True;否则为 false。

Windows 要求

设备系列
Windows 11 Insider Preview (在 10.0.23504.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v15.0 中引入)

示例

此示例演示了在独占控件中使用相机的应用程序如何确保相机配置在开始捕获之前设置,并且不会由有权访问此相机的另一个应用程序通过获取其前面的独占控制锁进行更改。

private static System.Threading.ManualResetEvent _exclusiveLockAcquire = new System.Threading.ManualResetEvent(false);

public static void RecordVideo()
{
    MediaCapture mediacapture = new MediaCapture();
    await mediacapture.InitializeAsync();

    mediacapture.CaptureDeviceExclusiveControlStatusChanged += 
Mediacapture_CaptureDeviceExclusiveControlStatusChanged;

    _exclusiveLockAcquire.WaitOne();
    _exclusiveLockAcquire.Reset();

    // configure camera - blocking other application from changing the configuration.

    // record video
}

private static void Mediacapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
{
    if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable)
    {
        if (sender.VideoDeviceController().TryAcquireExclusiveControl(
            args.DeviceId(),     
            MediaCaptureDeviceExclusiveControlReleaseMode.OnAllStreamsStopped))
        {
            _exclusiveLockAcquire.Set();
        }
    }
}

适用于