如何在Windows Phone 8.1中阅读音乐文件的完整元数据

时间:2014-11-21 10:59:48

标签: windows-phone-8.1

我正在尝试阅读Windows Phone 8.1中音乐文件的所有可用元数据。我只能得到名字,路径&创建音乐文件的日期。

我无法获得像专辑,艺术家,专辑艺术家,年份,出版商,作曲家,流派,持续时间,曲目编号,比特率,标题,评级等元数据。

我尝试了这个question中给出的解决方案。但它没有产生任何结果。

有谁知道如何实现这个目标?

    public class MusicFiles
    {
        public string fileName { get; set; }
        public string filePath { get; set; }
        public string dateCreated { get; set; }
    }

 IReadOnlyList<IStorageItem> fileList = await mFolder.GetItemsAsync();

 foreach(IStorageItem mItem in fileList)
               {
                   IStorageItem item = mItem;
           if(item.IsOfType(Windows.Storage.StorageItemTypes.File))
                       {
                           // create object of MusicAlbums() class.
                           MusicFiles musicAlbumObj = new MusicFiles();

                           // set name of item Folder.
                           musicAlbumObj.fileName = item.Name;

                           // set path of item Folder.
                           musicAlbumObj.filePath = item.Path;

                           // get item Folder's created date & Time.
                           musicAlbumObj.dateCreated = item.DateCreated.ToString();

                           string showText = "";
                           showText = musicAlbumObj.fileName + " *** " + musicAlbumObj.filePath + " *** " + musicAlbumObj.dateCreated;
                           MessageDialog msg = new MessageDialog(showText);
                           await msg.ShowAsync(); 

                       }
        }

2 个答案:

答案 0 :(得分:1)

我将TagLib用于我的项目。 您可以像这样使用它:

                using (var fs = await (item as StorageFile).OpenStreamForReadAsync())
                {
                    try
                    {
                        var tagFile = TagLib.File.Create(new StreamFileAbstraction(item.Name, fs, fs));
                        var tag = tagFile.GetTag(TagTypes.Id3v2);
                        if(tag.IsEmpty)
                        {
                            throw new ArgumentNullException(String.Format("No tag info found for {0}", item.Path));
                        }
                        var artistName = tag.FirstArtist;
                        var artist = CreateArtist(artistName);

                        var albumName = tag.Album;
                        var album = CreateAlbum(albumName, artist);

                        var trackTitle = tag.Title;
                        var track = CreateTrack(trackTitle, artist, album, item as StorageFile);
                    }
                    catch (Exception e)
                    {
                        var info = e.Message;
                        Debug.WriteLine(String.Format("Could not add the following file: {0}. Error: {1}.", item.Name, info));
                    }
                }

答案 1 :(得分:0)

foreach (var file in lstMusicFile)
{
    MusicProperties msProp = await file.Properties.GetMusicPropertiesAsync();
    DocumentProperties msDoc = await file.Properties.GetDocumentPropertiesAsync();
    MsMetadata msm = new MsMetadata();
    msm.Title = msProp.Title.Trim().Equals("") ? msProp.Subtitle : msProp.Title;
    msm.Artist = msProp.Artist.Trim().Equals("") ? "Unknown" : msProp.Artist;
    msm.Album = msProp.Album.Trim().Equals("") ? "Unknown" : msProp.Album;
    msm.Author = msDoc.Author.ElementAt(0).Trim().Equals("") ? "Unknown" : msDoc.Author.ElementAt(0);
    msm.Comment = msDoc.Comment.Trim().Equals("") ? "The Lyrics of this song will be update early !" : msDoc.Comment;
    msm.Source = file.Path;

    // Do something with msm                   
}