代码之家  ›  专栏  ›  技术社区  ›  mylim

UWP Windows 10 IOT 2通道同步录音

  •  0
  • mylim  · 技术社区  · 6 年前

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

    对于单通道,我的代码如下:

    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”,并选择相应的捕获设备。但是,有一个语法错误,我无法转换 enumaration.deviceinformation imediasource .

    captureInitSettings.AudioSource = captureDeviceList[audioCaptureList.SelectedIndex];
    

    更新:我设法让它工作 解决办法是

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

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

    更新: 我设法为保存应用程序设置 audiocapture 和; audiorender 但我不知道如何检索设备,也不知道是否保存了以前的设置。

    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;
            }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Michael Xu    6 年前

    您可以参考介绍如何 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);
        }