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

时间:2014-01-20 17:02:01

标签: c# windows-phone taglib-sharp

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您的代码无法编译,因为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 - 它就在你的样本中。

答案 2 :(得分:0)

是我,还是为什么TagLib.Create()不能简单地承受路径重载?为什么这么复杂? (至少我觉得这太困难了)