コード例 #1
0
        /// <summary>
        /// 从文件读取MP3FileInfo
        /// 文件不存在则返回null
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <returns>MP3FileInfo对象</returns>
        internal async Task <MP3FileInfo> LoadFromFile(string filepath)
        {
            if (!File.Exists(filepath) || !filepath.ToLower().EndsWith(".mp3"))
            {
                return(null);
            }
            Task <MP3FileInfo> task = new Task <MP3FileInfo>((object fpObj) =>
            {
                var fp              = fpObj.ToString();
                var bytes           = File.ReadAllBytes(fp);
                MP3FileInfo mp3Info = new MP3FileInfo();
                mp3Info.FileName    = Path.GetFileName(fp);
                mp3Info.FilePath    = Path.GetFullPath(fp);
                mp3Info.HaveID3     = checkID3(bytes);
                if (mp3Info.HaveID3)
                {
                    var tags = readFrames(bytes);
                    foreach (var key in tags.Keys)
                    {
                        switch (key)
                        {
                        case "TIT2":
                            mp3Info.Title = readFrameContent(tags[key]);
                            break;

                        case "TPE1":
                            mp3Info.Performer = readFrameContent(tags[key]);
                            break;

                        case "TALB":
                            mp3Info.Album = readFrameContent(tags[key]);
                            break;

                        case "TYER":
                            mp3Info.Year = readFrameContent(tags[key]);
                            break;

                        case "APIC":
                            mp3Info.Picture = readImage(tags[key]);
                            break;
                        }
                    }
                }
                if (string.IsNullOrEmpty(mp3Info.Album))
                {
                    mp3Info.Album = "未知";
                }
                if (string.IsNullOrEmpty(mp3Info.Performer))
                {
                    mp3Info.Performer = "未知";
                }

                return(mp3Info);
            }, filepath);

            task.Start();
            return(await task);
        }
コード例 #2
0
 private void AddDataToView(MP3FileInfo item)
 {
     System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
     if (item.Picture != null)
     {
         this.treeView1.ImageList.Images.Add(item.FilePath, item.Picture);
     }
     else
     {
         this.treeView1.ImageList.Images.Add(item.FilePath, ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))));
     }
     this.treeView1.Nodes.Add(new TreeNode(item.FileName)
     {
         Tag              = item,
         ImageKey         = item.FilePath,
         SelectedImageKey = item.FilePath,
         StateImageKey    = item.FilePath
     });
 }