2

我正在尝试使用 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 对象。我该如何进行这项工作?

4

1 回答 1

3

您的代码无法编译,因为 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 - 它在您的示例中。

于 2014-01-25T22:35:14.470 回答