[英]AudioDevice: recording sound using MediaCapture in WinRT/WP 8.1
I'm currently building an app that uses the WinRT MediaCapture API to record audio and take a photo. 我目前正在构建一个使用WinRT MediaCapture API录制音频和拍照的应用程序。
The code I have that initialises the MediaCapture and selects the right devices looks like this: 我拥有的用于初始化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);
I assumed, wrongly it seems, the first microphone would be the default. 我以为错误地认为,第一个麦克风将是默认麦克风。 However, one device I have found doesn't give you permission to use this microphone, it does however give access to the second.
但是,我发现其中一台设备没有授予您使用此麦克风的权限,但是可以访问另一台设备。 Clearly, this is not a reliable way of selecting a microphone.
显然,这不是选择麦克风的可靠方法。
Instead, I have now tried this code: 相反,我现在尝试了以下代码:
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);
Not setting an AudioDevice seems to still work using these settings but causes problem on the device I have (it crashes). 未设置AudioDevice似乎仍可以使用这些设置工作,但会导致我拥有的设备出现问题(崩溃)。
This could be device specific but I was wondering if there was a better or advised way of setting up the MediaCapture other than the above? 这可能是特定于设备的,但我想知道是否有除上述以外的更好或建议的设置MediaCapture的方法?
Try selecting your audio device by using the Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId
API. 尝试使用
Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId
API选择音频设备。
I would also strongly recommend you subscribe to the Windows.Media.Devices.MediaDevice.DefaultAudioCaptureDeviceChanged
event, to handle changing of the default audio capture device, if it applies to your scenario. 我也强烈建议您订阅
Windows.Media.Devices.MediaDevice.DefaultAudioCaptureDeviceChanged
事件,以处理默认音频捕获设备的更改(如果适用于您的方案)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.