【发布时间】:2015-08-28 01:56:30
【问题描述】:
我目前正在构建一个使用 WinRT MediaCapture API 来录制音频和拍照的应用程序。
初始化 MediaCapture 并选择正确设备的代码如下所示:
try
{
_captureManager = new MediaCapture();
string camera = "";
foreach (var device in await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
{
switch (device.EnclosureLocation.Panel)
{
case Windows.Devices.Enumeration.Panel.Back:
camera = device.Id;
continue;
}
}
var mics = await DeviceInformation.FindAllAsync(DeviceClass.AudioCapture);
var settings = new MediaCaptureInitializationSettings
{
PhotoCaptureSource = PhotoCaptureSource.Auto,
StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
VideoDeviceId = camera,
AudioDeviceId = mics.First().Id
};
await _captureManager.InitializeAsync(settings);
我错误地认为第一个麦克风是默认的。但是,我发现的一个设备不允许您使用此麦克风,但它确实允许访问第二个设备。显然,这不是选择麦克风的可靠方法。
相反,我现在尝试了以下代码:
try
{
_captureManager = new MediaCapture();
var camera = "";
foreach (var device in await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
{
switch (device.EnclosureLocation.Panel)
{
case Windows.Devices.Enumeration.Panel.Back:
camera = device.Id;
continue;
}
}
var settings = new MediaCaptureInitializationSettings
{
PhotoCaptureSource = PhotoCaptureSource.Auto,
StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
VideoDeviceId = camera,
};
await _captureManager.InitializeAsync(settings);
不设置 AudioDevice 似乎仍然可以使用这些设置,但会导致我拥有的设备出现问题(它崩溃)。
这可能是特定于设备的,但我想知道除上述方法之外是否有更好或建议的方法来设置 MediaCapture?
【问题讨论】:
-
我相信如果你没有在
MediaCaptureInitializationSettings上设置*DeviceId,它会回退到默认值。DeviceInformation中的设备排序可能没有任何意义。现在,当您说它崩溃时,您在拨打InitializeAsync时是否有异常? -
@Mike 我怀疑可能是这种情况,崩溃的手机返回一个异常,说不允许访问该设备。
-
您是否在清单中声明了必要的功能?
-
@Mike,不幸的是,它们是麦克风、相机、存储等都被检查了。
标签: c# windows-runtime windows-phone