简体   繁体   English

AudioDevice:在WinRT / WP 8.1中使用MediaCapture录制声音

[英]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.

相关问题 MediaCapture更改相机焦点(WP8.1和WinRt8.1) - MediaCapture change camera focus(WP8.1 and WinRt8.1) 订阅MediaCapture RecordLimitation超过了录制音频的速度(WinRT) - Subscribing MediaCapture RecordLimitationExceeded for recording audio (WinRT) 如何使用现有的相机应用程序(不是使用 MediaCapture 创建一个)并在 windows phone 8.1(WinRT) 中拍照? - How to use existing camera app(not create one using MediaCapture) and take picture in windows phone 8.1(WinRT)? 使用Async Task with Dispatcher从ViewModel中为WP8.1 WinRT循环更新UI线程 - Using Async Task with Dispatcher to update UI thread in loop for WP8.1 WinRT from ViewModel 是否可以使用WinRT中的MediaCapture将视频直接捕获到SQL数据库中? - Is it possible to Capture a Video directly into an SQL database using MediaCapture in WinRT? WP8.1RT:来自mediacapture C#的自定义分辨率图像 - WP8.1RT: Custom resolution image taken from mediacapture C# Winrt / WP8.1中长时间轮询请求的巨大延迟 - Huge delay in long polling request in Winrt/WP8.1 WinRT / WP 8.1中的默认文件路径是什么? - What's the default file path in WinRT/WP 8.1? WP 8.1 WinRT-地图上的图钉自定义工具提示(或UserControl)? - WP 8.1 WinRT - Custom tooltip (or UserControl) in maps for a pushpin? 在Wp 8.1 WinRT Phone应用程序中调用Wp 8.1 Silverlight电话应用程序 - Calling Wp 8.1 Silverlight phone app in Wp 8.1 WinRT Phone app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM