Share via


VideoDeviceController.TryAcquireExclusiveControl 方法

定義

要求具有指定裝置識別碼之相機的獨佔控制。

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

要求獨佔控制項之相機的裝置識別碼。 您可以使用 DeviceInformation 類別來取得裝置識別碼。

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

適用於