[英]taglib-sharp: Retrieving the ChannelMode of an MP3 File
I am using the taglib-sharp library in my C# Win Forms application to retrieve the duration and bit rate of MP3 files. 我在C#Win Forms应用程序中使用taglib-sharp库来检索MP3文件的持续时间和比特率。 A code snippet follows: 一个代码片段如下:
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");
I would now like to also determine if the file is Mono or Stereo. 我现在还要确定文件是Mono还是Stereo。 To do that, I think I need to read the ChannelMode (0 = Stereo, 1 = JointStereo, 2 = DualChannel, 3 = SingleChannel). 为此,我认为我需要阅读ChannelMode(0 =立体声,1 = JointStereo,2 = DualChannel,3 = SingleChannel)。 The only problem is that I don't know how to access it. 唯一的问题是我不知道如何访问它。 When I debug the code, I can see ChannelMode in the watch window . 调试代码时,可以在监视窗口中看到ChannelMode 。
Yet accessing it is proving difficult. 但是事实证明,访问它很困难。 I only got this far: 我只有这么远:
var codec = (((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0));
When I run this, I can see codec in the debugger's watch window , and under it is ChannelMode . 运行此命令时,我可以在调试器的监视窗口中看到编解码器 ,并且在其下方是ChannelMode 。
I am inclined to think that I should just be able to read codec.ChannelMode at this point, but that's clearly not the right syntax. 我倾向于认为我现在应该只能阅读codec.ChannelMode ,但这显然不是正确的语法。 I get this compiler error: 我收到此编译器错误:
Error CS1061 'object' does not contain a definition for 'ChannelMode' and no extension method 'ChannelMode' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 错误CS1061'对象'不包含'ChannelMode'的定义,并且找不到扩展方法'ChannelMode'接受类型为'object'的第一个参数(您是否缺少using指令或程序集引用?)
What am I doing wrong? 我究竟做错了什么?
Thanks in advance, 提前致谢,
Mike. 麦克风。
GetValue(0)
returns a type of object
. GetValue(0)
返回object
的类型。 You will need to cast the return value to an appropriate type. 您将需要将返回值转换为适当的类型。 In this case probably an AudioHeader
(implements ICodec
) which has a ChannelMode
property. 在这种情况下,可能是具有ChannelMode
属性的AudioHeader
(实现ICodec
)。 Like so 像这样
var codec = (AudioHeader)(((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0));
Or safer 或更安全
var codec = (((TagLib.ICodec[])tagFile.Properties.Codecs).GetValue(0)) as AudioHeader?;
if (codec != null)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.