简体   繁体   English

访问音乐文件UWP -KnownFolders.MusicLibrary.GetItemsAsync()不返回任何内容

[英]Access music files UWP -KnownFolders.MusicLibrary.GetItemsAsync() doesnt return anything

Hey i have the following : 嘿,我有以下内容:

 var fileList = await KnownFolders.MusicLibrary.GetItemsAsync();

It doesn't return any files and i have files in the Music folder. 它不返回任何文件,我在“音乐”文件夹中有文件。 I also have : 我也有 :

<Capabilities>
<Capability Name="internetClient" />
<Capability Name="removableStorage" />
<Capability Name="documentsLibrary" />
<Capability Name="MusicLibrary" />
<Capability Name="HomeGroup" />
<Capability Name="RemovableDevices" />

I dont know why it doesnt return any files/ exception ? 我不知道为什么它不返回任何文件/异常? Any suggestions ? 有什么建议么 ? I also tried FolderPicker to get all files in a folder , but same result. 我也尝试了FolderPicker来获取文件夹中的所有文件,但结果相同。

Form the code you've posted, it seems you are using wrong capabilities. 从您发布的代码来看,您似乎使用了错误的功能。

The musicLibrary capability must include the uap namespace when you declare it in your app's package manifest as shown below. 当您在应用的程序包清单中声明musicLibrary功能时,它必须包含uap命名空间,如下所示。

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

For more info, please see App capability declarations . 有关更多信息,请参阅App功能声明

So you can chane your Package.appxmanifest like the following: 因此,您可以像下面这样处理Package.appxmanifest

<Capabilities>
  <Capability Name="internetClient" />
  <uap:Capability Name="removableStorage" />
  <uap:Capability Name="musicLibrary" />
  <uap:Capability Name="documentsLibrary" />
</Capabilities>

And then you should be able to get the files and subfolders in the music library. 然后,您应该能够在音乐库中获取文件和子文件夹。

var fileList = await KnownFolders.MusicLibrary.GetItemsAsync();
if (fileList.Count > 0)
{
    foreach (var item in fileList)
    {
        Debug.WriteLine(item.Name);
    }
}

The Music Library typically has the following path. 音乐库通常具有以下路径。

%USERPROFILE%\Music

You can check if you have files under this path. 您可以检查此路径下是否有文件。 And also you can check the path with the following code. 您也可以使用以下代码检查路径。

var musicLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);
Debug.WriteLine(musicLibrary.SaveFolder.Path);

This will output the path of the known folder, which is the folder in a library where new files are saved by default. 这将输出已知文件夹的路径,该文件夹是库中默认保存新文件的文件夹。 For more info, please see this answer . 有关更多信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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