Taglib-sharp: how to use the IFileAbstraction to allow reading metadata from stream?

我怕爱的太早我们不能终老 提交于 2019-12-08 09:54:24

问题


I'm trying to read the metadata of a mp3 file stored in IsolatedStorage using TagLib. I know TagLib normally only take a file path as input but as WP uses a sandbox environment I need to use a stream.

Following this tutorial (http://www.geekchamp.com/articles/reading-and-writing-metadata-tags-with-taglib) I created a iFileAbstraction interface:

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;
    }
}

Normally I would now be able to do this:

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);
}

The problem is that TagLib.File.Create still doesn't want to accept the SimpleFile object. How do I make this work?


回答1:


Your code doesn't compile because TagLib.File.Create wants IFileAbstraction on input, and you're giving it SimpleFile instance which doesn't implement the interface. Here's one way to fix:

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

Don't ask me why we need SimpleFile class instead of passing name and stream into SimpleFileAbstraction - it was in your sample.




回答2:


You can try this: MusicProperties class Should be enough for you, and usage is way easier.



来源:https://stackoverflow.com/questions/21239918/taglib-sharp-how-to-use-the-ifileabstraction-to-allow-reading-metadata-from-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!