gpt4 book ai didi

c# - 获取最近听过的音乐列表

转载 作者:行者123 更新时间:2023-11-30 12:45:16 25 4
gpt4 key购买 nike

我正在开发一个 Windows Phone 应用程序,它需要检索和操作有关设备上播放的歌曲的信息。

我知道可以使用 MediaPlayer.Queue.ActiveSong 获取当前正在播放的歌曲。

但是,我真正需要的是能够访问最近播放轨道的列表。

MediaHistoryMediaHistoryItem 类似乎没有提供此功能。

真的可以吗?怎么办?

最佳答案

正如@Igor 在他的回答中指出的那样,当前的 API 不允许这样做。但是,还有另一种方法可以让我们合理地假设最近播放了一个特定的媒体文件,方法是获取有关实际文件的一些信息。

我们可以将 GetBasicPropertiesAsync()RetrievePropertiesAsync() 一起使用,这将为我们提供该文件的 DateAccessed 属性。

这是摘自 this 的代码片段MSDN 页面:

public async void test()
{

try
{
StorageFile file = await StorageFile.GetFileFromPathAsync("Filepath");
if (file != null)
{
StringBuilder outputText = new StringBuilder();

// Get basic properties
BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
outputText.AppendLine("File size: " + basicProperties.Size + " bytes");
outputText.AppendLine("Date modified: " + basicProperties.DateModified);

// Specify more properties to retrieve
string dateAccessedProperty = "System.DateAccessed";
string fileOwnerProperty = "System.FileOwner";
List<string> propertiesName = new List<string>();
propertiesName.Add(dateAccessedProperty);
propertiesName.Add(fileOwnerProperty);

// Get the specified properties through StorageFile.Properties
IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);
var propValue = extraProperties[dateAccessedProperty];
if (propValue != null)
{
outputText.AppendLine("Date accessed: " + propValue);
}
propValue = extraProperties[fileOwnerProperty];
if (propValue != null)
{
outputText.AppendLine("File owner: " + propValue);
}
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle a file not found error
}
}

一旦您在变量中拥有 DateAccessed 属性,我们就可以查看它是否是最近的日期,比如昨天,甚至可能是 2 或 3 天前。然后我们就会知道,如果它在短时间内被访问过,它就可以播放。

不过,对此有一些注意事项。一些病毒扫描程序会更改文件和文件夹的时间戳属性,它们还需要打开文件进行扫描,我认为这会更改 DateAccessed 属性。但是,我见过的许多新的防病毒应用程序会将时间戳信息恢复为原始信息,就好像它从未接触过文件一样。

我相信这是目前解决此问题的最佳方法。 除非您只关心您的应用最近何时播放文件。那么这个问题的答案就像您管理自己最近播放的媒体文件列表一样简单。

更新


为了检索指定歌曲的 PlayCount,您可以使用 MediaLibrary 类访问该歌曲:

MediaLibrary library = new MediaLibrary();

然后像这样访问歌曲:

Int32 playCount = library.Songs[0].PlayCount;

其中 [0] 是您要为其获取 PlayCount 的歌曲的索引。一种更简单的方法(取决于您已经访问歌曲的方式,可能是做类似的事情:

Int32 playCount = library.Artists[selectedArtistIndex].Albums[selectedArtistAlbumIndex].Songs[selectedSongInAlbumIndex].PlayCount;

关于c# - 获取最近听过的音乐列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com