c# – 获取最近收听的音乐列表
我正在开发一个
Windows Phone应用程序,需要检索和操作有关设备上播放的歌曲的信息.
我知道可以使用MediaPlayer.Queue.ActiveSong获取当前正在播放的歌曲. 但是,我真正需要的是能够访问最近播放的曲目列表. MediaHistory和MediaHistoryItem类似乎不提供此功能. 真的有可能吗?怎么样? 解决方法
正如@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天前.然后我们就会知道,如果它在很短的时间内被访问过,它就可以播放了. 不过,有一些警告.某些病毒扫描程序会更改文件和文件夹上的Timestamp属性,并且还需要打开文件来扫描它们,我认为这会更改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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |