访问音乐文件 UWP

编程入门 行业动态 更新时间:2024-10-28 14:36:45
本文介绍了访问音乐文件 UWP -KnownFolders.MusicLibrary.GetItemsAsync() 不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

嘿,我有以下内容:

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

它不返回任何文件,我在 Music 文件夹中有文件.我也有:

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" />

我不知道为什么它不返回任何文件/异常?有什么建议 ?我也尝试过 FolderPicker 来获取文件夹中的所有文件,但结果相同.

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.

推荐答案

根据您发布的代码,您似乎使用了错误的功能.

Form the code you've posted, it seems you are using wrong capabilities.

当您在应用的包清单中声明 musicLibrary 功能时,它必须包含 uap 命名空间,如下所示.

The musicLibrary capability must include the uap namespace when you declare it in your app's package manifest as shown below.

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

有关详细信息,请参阅应用功能声明.

For more info, please see App capability declarations.

所以你可以像下面这样修改你的Package.appxmanifest:

So you can chane your Package.appxmanifest like the following:

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

这篇关于访问音乐文件 UWP -KnownFolders.MusicLibrary.GetItemsAsync() 不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 19:15:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1122195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:音乐文件   UWP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!