我想提供一個選項,用於在使用CaptureElement/MediaCapture顯示預覽的網絡攝像頭之間進行切換。不幸的是,我嘗試了呼叫序列的多種組合,預覽只顯示了我使用的第一個設備。如何在使用CaptureElement/MediaCapture時切換網絡攝像頭?
這是我一直在努力做的事情:
XAML:
<CaptureElement
x:Name="captureElement"
Stretch="UniformToFill" />
C#:
MediaCapture mediaCapture;
DeviceInformationCollection devices;
int currentDevice = 0;
private async void LayoutRoot_Tapped(object sender, Windows.UI.Xaml.Input.TappedEventArgs e)
{
if (devices != null)
{
currentDevice = (currentDevice + 1) % devices.Count;
InitializeWebCam();
}
}
private async void InitializeWebCam()
{
if (devices == null)
{
devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
ListDeviceDetails();
}
if (mediaCapture != null)
{
await mediaCapture.StopPreviewAsync();
this.captureElement.Source = null;
}
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(
new MediaCaptureInitializationSettings
{
VideoDeviceId = devices[currentDevice].Id
});
this.captureElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
}
private void ListDeviceDetails()
{
int i = 0;
foreach (var device in devices)
{
Debug.WriteLine("* Device [{0}]", i++);
Debug.WriteLine("EnclosureLocation.InDock: " + device.EnclosureLocation.InDock);
Debug.WriteLine("EnclosureLocation.InLid: " + device.EnclosureLocation.InLid);
Debug.WriteLine("EnclosureLocation.Panel: " + device.EnclosureLocation.Panel);
Debug.WriteLine("Id: " + device.Id);
Debug.WriteLine("IsDefault: " + device.IsDefault);
Debug.WriteLine("IsEnabled: " + device.IsEnabled);
Debug.WriteLine("Name: " + device.Name);
Debug.WriteLine("IsDefault: " + device.IsDefault);
foreach (var property in device.Properties)
{
Debug.WriteLine(property.Key + ": " + property.Value);
}
}
}
好像它的工作,以在一旦切換到第二個攝像頭同時(低於10%的時間),然後在我回到第一個時保持全黑。
有時應用程序掛起後,我嘗試切換相機一次或兩次(停止響應輸入,它是停留在App.Run(),雖然攝像頭預覽不斷刷新)。
其他時候 - 它工作的方式,它顯示從第一設備預覽,但不爲其他一個,當我再回到第一個 - 它再次工作與它的罰款。
錯誤?
似乎有不是一個處置或未初始化方法的任何地方。這是我看到的性能(這是三星的Build 2011片):
* Device [0]
EnclosureLocation.InDock: False
EnclosureLocation.InLid: False
EnclosureLocation.Panel: Front
Id: \\?\USB#VID_2232&PID_1021&MI_00#7&2469C269&0&0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
IsDefault: False
IsEnabled: True
Name: WebCam SC-20FHM11347N
IsDefault: False
System.ItemNameDisplay: WebCam SC-20FHM11347N
System.Devices.DeviceInstanceId: USB\VID_2232&PID_1021&MI_00\7&2469C269&0&0000
System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
System.Devices.InterfaceEnabled: True
System.Devices.IsDefault: False
* Device [1]
EnclosureLocation.InDock: False
EnclosureLocation.InLid: False
EnclosureLocation.Panel: Back
Id: \\?\USB#VID_2232&PID_1022&MI_00#7&27072759&0&0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
IsDefault: False
IsEnabled: True
Name: WebCam SC-30H2L11449N
IsDefault: False
System.ItemNameDisplay: WebCam SC-30H2L11449N
System.Devices.DeviceInstanceId: USB\VID_2232&PID_1022&MI_00\7&27072759&0&0000
System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
System.Devices.InterfaceEnabled: True
System.Devices.IsDefault: False
謝謝你 - 它並沒有真正解決我的問題,但這些都是有用的技巧。 – 2012-02-04 03:58:25