Search code examples
c#xamluwpraspberry-pi3windows-iot-core-10

UWP C# To Display AudioDevice DeviceInformation to Textblock


I am having some issue to display my saved USB Audio Adapter name on a textblockwhen i load it from ApplicationDataContainer

when selecting the from the listbox, the deviceIdis able to be displayed properly on the 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;
        }

enter image description here

However, when loading of saved audio deviceId it couldn't be displayed as readable characters;

    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 ..";

enter image description here

Please help. Thank you.


Solution

  • In SelectionChanged event, you are displaying the Name and saving the Id. But when loading, you are showing Id, they are not same.

    When loading, you can change code to:

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

    Best regards.