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

C# Taglib sharp:如何使用IFileAbstraction允许从流中读取元数据?,c#,windows-phone,taglib-sharp,C#,Windows Phone,Taglib Sharp,我正在尝试使用TagLib读取存储在IsolatedStorage中的mp3文件的元数据。 我知道TagLib通常只接受文件路径作为输入,但由于WP使用沙箱环境,我需要使用流 在本教程()之后,我创建了IFileAbstration接口: public class SimpleFile { public SimpleFile(string Name, Stream Stream) { this.Name = Name; this.Stream =

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

在本教程()之后,我创建了IFileAbstration接口:

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对象。 我该如何使其工作?

您可以尝试以下方法:
对您来说应该足够了,而且使用起来更简单。

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

是我吗,或者为什么TagLib.Create()不能简单地拥有一个重载来获取路径?为什么这么复杂?(至少我觉得这太难了)

我只将音乐文件作为流保存在隔离存储中。我怎么能把它用在音乐节目上?你不能。
StorageItemContentProperties.GetMusicPropertiesAsync
IStorageItemInformation.MusicProperties
均未在windows phone平台上实现。是的,抱歉。你不能。我在Win8应用程序中使用它。MSDN上提到MusicProperties类的页面上说,您可以在WP8上使用它,但随后它说没有实现方法…我可以在控制台应用程序中使用MusicProperties类吗?谢谢,但现在我在“TagLib.Tag tags=TagLib.File.Create(file2);”上得到System.TypeLoadException查看调试器中的异常:它通常会告诉您加载失败的类型以及原因。“程序集中的类型‘TagLib.Id3v2.Tag’TagLib sharp,Version=2.1.0.0,Culture=neutral,PublicKeyToken=db62eba44689b5b0’正试图实现一个不可访问的接口。”嗯,看起来TagLib与windows phone不兼容。TagLib是开源的,所以您可以调试它(我从这个选项开始,但不会在这方面花费超过几个小时),或者(如果许可证允许)将相关的源文件复制粘贴到您的项目中,或者使用TagLib作为参考和测试创建您自己的解决方案。