UWP C# 将 AudioDevice 设备信息显示到文本块

UWP C# To Display AudioDevice DeviceInformation to Textblock

当我从 ApplicationDataContainer

加载时,我无法在 textblock 上显示我保存的 USB 音频适配器名称

listbox中选择时,deviceId可以在textblock

上正常显示
private void audioRenderList_P_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        mediaPlayer_CH1.AudioDevice = renderDeviceList_P[audioRenderList_P.SelectedIndex];
        renderDeviceName_P.Text = renderDeviceList_P[audioRenderList_P.SelectedIndex].Name.ToString();
    }

if (mediaPlayer_CH1.AudioDevice.Id != null)
        {
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            localSettings.Values["audioRenderSettings_P"] = mediaPlayer_CH1.AudioDevice.Id;
        }

但是,在加载保存的音频时deviceId它无法显示为可读字符;

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

    if (localSettings.Values["audioRenderSettings_P"] != null)
    {
        var audioSource = localSettings.Values["audioRenderSettings_P"] as string;
        mediaPlayer_CH1.AudioDevice = await DeviceInformation.CreateFromIdAsync(audioSource);
        renderDeviceName_P.Text = audioSource;
    }
    else renderDeviceName_P.Text = "Select Audio Device ..";

请帮忙。谢谢。

SelectionChanged 事件中,您正在显示 Name 并保存 Id。但是在加载时,你显示的是 Id,它们不一样。

加载时,您可以将代码更改为:

var audioSource = localSettings.Values["audioRenderSettings_P"] as string;
mediaPlayer_CH1.AudioDevice = await DeviceInformation.CreateFromIdAsync(audioSource);
renderDeviceName_P.Text = mediaPlayer_CH1.AudioDevice.Name.ToString();

此致。