How can I display all files in Music Library on startup of app in UWP

142 Views Asked by At

I am trying to display all music files (Mp3) that is in the Music folder in Windows IoT Core on startup of the app in my ListView.

What I have at the moment is:

public MainPage()
    {
        this.InitializeComponent();
        InitGPIO();
        ListfilesAsync();
        Unloaded += MainPage_Unloaded;

    }

 private async Task ListfilesAsync()
    {
        var folderPicker = new Windows.Storage.Pickers.FolderPicker();
        folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
        folderPicker.FileTypeFilter.Add("*.mp3");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();

        List<string> fileTypeFilter = new List<string>();
        fileTypeFilter.Add(".mp3");

        if (folder != null)
        {
            QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
            StorageFileQueryResult results = folder.CreateFileQueryWithOptions(queryOptions);
            IReadOnlyList<StorageFile> sortedFiles = await results.GetFilesAsync();
            foreach (StorageFile item in sortedFiles)
            {
                AudioFilesLV.Items.Add(item.Path.ToString());
            }
        }
    }

The app starts with no errors but there is nothing displayed in the ListView?

Not sure if it is being initialized correctly or there is fault with my code?

Thanks

1

There are 1 best solutions below

0
Michael Xu On

FolderPicker and FolderOpenPicker can not work on Windows IoT Core. You can use KnownFolders.MusicLibrary to list the file in music library.

            var files = await Windows.Storage.KnownFolders.MusicLibrary.GetFilesAsync();
            foreach (StorageFile item in files)
            {
                AudioFilesLV.Items.Add(item.Path.ToString());
            }

Please note that, you need to specify the Music Library capability in Package.appxmanifest.

  <Capabilities>
    <uap:Capability Name="musicLibrary"/>
  </Capabilities>