【问题标题】:taglib-sharp: Retrieving the ChannelMode of an MP3 Filetaglib-sharp:检索 MP3 文件的 ChannelMode
【发布时间】:2017-10-26 01:31:44
【问题描述】:

我在我的 C# Win Forms 应用程序中使用 taglib-sharp 库来检索 MP3 文件的持续时间和比特率。代码 sn-p 如下:

TagLib.File tagFile = TagLib.File.Create(myMp3FileName);

int bitrate = tagFile.Properties.AudioBitrate;
string duration = tagFile.Properties.Duration.Hours.ToString("D2") + ":" +
                  tagFile.Properties.Duration.Minutes.ToString("D2") + ":" +
                  tagFile.Properties.Duration.Seconds.ToString("D2");

我现在还想确定文件是单声道还是立体声。为此,我想我需要阅读 ChannelMode(0 = Stereo,1 = JointStereo,2 = DualChannel,3 = SingleChannel)。唯一的问题是我不知道如何访问它。调试代码的时候可以看到ChannelMode in the watch window

但事实证明,访问它很困难。我只做到了这一点:

var codec = (((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0));

当我运行这个时,我可以看到codec in the debugger's watch window,在它下面是ChannelMode

我倾向于认为此时我应该能够阅读 codec.ChannelMode,但这显然不是正确的语法。我得到这个编译器错误:

错误 CS1061“object”不包含“ChannelMode”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“ChannelMode”(您是否缺少 using 指令或程序集引用?)

我做错了什么?

提前致谢,

迈克。

【问题讨论】:

    标签: c# taglib taglib-sharp


    【解决方案1】:

    GetValue(0) 返回object 的类型。您需要将返回值转换为适当的类型。在这种情况下,可能是具有 ChannelMode 属性的 AudioHeader(实现 ICodec)。像这样

    var codec = (AudioHeader)(((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0));
    

    或者更安全

    var codec = (((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0)) as AudioHeader?;
    if (codec != null)
        ...
    

    【讨论】:

    • 谢谢本!您的第一个解决方案效果很好!但是,无论如何,第二种解决方案会产生编译器错误Error CS0077 The as 运算符必须与引用类型或可为空的类型一起使用('AudioHeader' 是不可为空的值类型)。我在这里有点新手,但是工作的解决方案是否不安全?
    • 哦,我没注意到AudioHeader 是一个结构。第一种方法很好。你只是想确保你投射到正确的类型。
    • 如果你把它变成Nullable(... as AudioHeader?),你仍然可以使用第二种方法(更新答案)
    猜你喜欢
    • 2011-09-05
    • 2019-01-27
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多