简体   繁体   中英

UWP C# To Display AudioDevice DeviceInformation to Textblock

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

when selecting the from the listbox , the deviceId is 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;
        }

在此处输入图像描述

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

在此处输入图像描述

Please help. Thank you.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM