【问题标题】:AudioDevice: recording sound using MediaCapture in WinRT/WP 8.1AudioDevice:在 WinRT/WP 8.1 中使用 MediaCapture 录制声音
【发布时间】: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


【解决方案1】:

尝试使用Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId API 选择您的音频设备。

我还强烈建议您订阅 Windows.Media.Devices.MediaDevice.DefaultAudioCaptureDeviceChanged 事件,以处理默认音频捕获设备的更改(如果适用于您的场景)。

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多