UWP Windows 10 物联网 2 通道同时录音

UWP Windows 10 IoT 2 channel Simultaneous Audio Recording

我想知道如何同时录制2个独立的音频通道。 我有 2 个 USB 适配器,分别带有麦克风和扬声器。 我能找到的示例代码一次只支持单通道录制。 请帮忙。谢谢。

对于单通道我的代码如下;

MediaCapture audioCapture = new MediaCapture();
MediaCaptureInitializationSettings captureInitSettings = new MediaCaptureInitializationSettings();

captureInitSettings.StreamingCaptureMode = StreamingCaptureMode.Audio;
captureInitSettings.MediaCategory = MediaCategory.Other;
captureInitSettings.AudioProcessing = AudioProcessing.Default;

await audioCapture.InitializeAsync(captureInitSettings);

private async void recordChannelA()
    {
        StorageFolder externalDevices = KnownFolders.RemovableDevices;
        IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
        StorageFolder usbStorage = externalDrives[0];

        if (usbStorage != null)
        {
            StorageFolder recordFolder = await usbStorage.CreateFolderAsync(recFolderName, CreationCollisionOption.OpenIfExists);
            await usbStorage.GetFolderAsync(recFolderName);
            StorageFile recordFile = await recordFolder.CreateFileAsync("Recording - " + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".mp3", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

            MediaEncodingProfile profile = null;
            profile = MediaEncodingProfile.CreateM4a(Windows.Media.MediaProperties.AudioEncodingQuality.Auto);
            await audioCapture.StartRecordToStorageFileAsync(profile, recordFile);

            Message.Text = "Recording ... ";

            recordingtimerRun = new TimeSpan(0, 0, 0);
            recordingTimer.Start();
        }
        else Message.Text = "Recording error !";
    }

更新; 我为枚举设备创建了 'listview',并为相应的捕获设备创建了 select。但是,存在语法错误,我无法将 enumaration.deviceinformation 转换为 imediasource

captureInitSettings.AudioSource = captureDeviceList[audioCaptureList.SelectedIndex];

更新:我设法让它工作了 解决方案是

    captureInitSettingsA.AudioDeviceId = captureDeviceList[audioCaptureList.SelectedIndex].Id;
captureInitSettingsB.AudioDeviceId = captureDeviceList[audioCaptureList.SelectedIndex].Id;

但是,我如何在应用程序设置中保存这些 selection .. 这样当我重新启动时我就不必再重新 select 了。

更新: 我设法保存了 audiocaptureaudiorender 设备的应用程序设置,但我不确定如何检索它们以及检查是否保存了任何以前的设置。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            localSettings.Values["audioACaptureSettings"] = captureAInitSettings.AudioDeviceId;
            localSettings.Values["audioARenderSettings"] = mediaPlayerA.AudioDevice.Id;
localSettings.Values["audioBCaptureSettings"] = captureBInitSettings.AudioDeviceId;
            localSettings.Values["audioBRenderSettings"] = mediaPlayerB.AudioDevice.Id;



private void loadAudioConfig()
    {
        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

        if (localSettings.Values["audioACaptureSettings"] != null)
        {
            captureAInitSettings.AudioDeviceId = localSettings.Values["audioACaptureSettings"].ToString();
        }

        if (localSettings.Values["audioARenderSettings"] != null)
        {
            Object audioARenderValue = localSettings.Values["audioARenderSettings"];
            mediaPlayerA.AudioDevice = audioARenderValue;
        }

        if (localSettings.Values["PAaudioCaptureSettings"] != null)
        {
            captureBInitSettings.AudioDeviceId = localSettings.Values["audioBCaptureSettings"].ToString();
        }

        if (localSettings.Values["PAaudioRenderSettings"] != null)
        {
            Object audioBRenderValue = localSettings.Values["audioBRenderSettings"];
            mediaPlayerB.AudioDevice = audioBRenderValue;
        }

您可以参考这篇介绍如何store and retrieve settings and other app data的文档。您可以将数据保存到设置和文件。 当您使用设置时,它仅支持文档中提到的多种数据类型。 如果使用文件,您可以存储二进制数据或启用您自己的自定义序列化类型。

在您提供的代码中,检查是否保存了之前的设置是正确的:

    if (localSettings.Values["audioACaptureSettings"] != null)
    {
        captureAInitSettings.AudioDeviceId = localSettings.Values["audioACaptureSettings"].ToString();
    }

但是关于如何将设置检索为 AudioDevice 是不正确的,因为它不能将字符串隐式转换为 DeviceInformation。请这样尝试:

    if (localSettings.Values["audioARenderSettings"] != null)
    {
        var aduioSource = localSettings.Values["audioARenderSettings"] as string;
        mediaPlayerA.AudioDevice = await DeviceInformation.CreateFromIdAsync(aduioSource);
    }