个人资料

跳过导航链接首页 > 博客列表 > 博客正文

c#给mp3文件添加图片(专辑封面)

分类:

代码环境 : UWP

使用nuget 工具包:TagLib#.Portable

示例代码:

        public static async void AddImage4Mp3(StorageFile img, StorageFile musicFile)
        {
            try
            {
                var musicStream = await musicFile.OpenAsync(FileAccessMode.ReadWrite);
 
                var tagFile = TagLib.File.Create(

                    new StreamFileAbstraction(
                        musicFile.Name,
                        musicStream.AsStream(),
                        musicStream.AsStream()));

                var tags = tagFile.GetTag(TagTypes.Id3v2);

                Debug.WriteLine(tags.Title);
                Debug.WriteLine(tags.Album);
                Debug.WriteLine(tags.Comment);
                Debug.WriteLine(tags.FirstPerformer);
                Debug.WriteLine(tags.Lyrics);
                Debug.WriteLine(tags.Year);

                var imageStream = await img.OpenAsync(FileAccessMode.ReadWrite);

                Picture picture = new Picture(new StreamFileAbstraction(img.Name, imageStream.AsStream(), imageStream.AsStream()));
                picture.Type = PictureType.FrontCover;
                picture.MimeType = img.ContentType;
                picture.Description = "Front Cover";
                tagFile.Tag.Pictures = new IPicture[1] { picture };
                Debug.WriteLine(3);
                tagFile.Save();
                Debug.WriteLine(4);
                musicStream?.Dispose();
                
                imageStream?.Dispose();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                
            }


        }


songshizhao
最初发表2021/7/2 19:06:15 最近更新2021/7/2 19:07:18 1321
为此篇作品打分
10
   评论