Taglib-sharp:如何使用IFileAbstraction来允许从流中读取元数据?

我正在尝试使用TagLib读取存储在IsolatedStorage中的mp3文件的元数据。 我知道TagLib通常只将文件路径作为输入,但是当WP使用沙箱环境时我需要使用流。

按照本教程( http://www.geekchamp.com/articles/reading-and-writing-metadata-tags-with-taglib ),我创建了一个iFileAbstraction接口:

public class SimpleFile { public SimpleFile(string Name, Stream Stream) { this.Name = Name; this.Stream = Stream; } public string Name { get; set; } public Stream Stream { get; set; } } public class SimpleFileAbstraction : TagLib.File.IFileAbstraction { private SimpleFile file; public SimpleFileAbstraction(SimpleFile file) { this.file = file; } public string Name { get { return file.Name; } } public System.IO.Stream ReadStream { get { return file.Stream; } } public System.IO.Stream WriteStream { get { return file.Stream; } } public void CloseStream(System.IO.Stream stream) { stream.Position = 0; } } 

通常我现在可以这样做:

 using (IsolatedStorageFileStream filestream = new IsolatedStorageFileStream(name, FileMode.OpenOrCreate, FileAccess.ReadWrite, store)) { filestream.Write(data, 0, data.Length); // read id3 tags and add SimpleFile newfile = new SimpleFile(name, filestream); TagLib.Tag tags = TagLib.File.Create(newfile); } 

问题是TagLib.File.Create仍然不想接受SimpleFile对象。 我该如何工作?

你可以尝试这个: MusicProperties类应该足够你,使用起来更容易。

您的代码无法编译,因为TagLib.File.Create在输入上需要IFileAbstraction,而您正在为它提供不实现接口的SimpleFile实例。 这是一种解决方法:

 // read id3 tags and add SimpleFile file1 = new SimpleFile( name, filestream ); SimpleFileAbstraction file2 = new SimpleFileAbstraction( file1 ); TagLib.Tag tags = TagLib.File.Create( file2 ); 

不要问我为什么我们需要SimpleFile类而不是将名称和流传递​​给SimpleFileAbstraction – 它就在你的样本中。