本文介绍了Taglib-sharp:如何使用 IFileAbstraction 来允许从流中读取元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

问题是 TagLib.File.Create 仍然不想接受 SimpleFile 对象.我该怎么做?

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

推荐答案

您的代码无法编译,因为 TagLib.File.Create 需要 IFileAbstraction 输入,而您给它的 SimpleFile 实例没有实现接口.这是一种解决方法:

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

不要问我为什么需要 SimpleFile 类而不是将名称和流传递​​到 SimpleFileAbstraction 中 - 它在您的示例中.

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

这篇关于Taglib-sharp:如何使用 IFileAbstraction 来允许从流中读取元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 15:31