代码环境 : 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); } }