可以在不使用Windows Media Player的情况下获取WMA文件的长度?

[英]Possible to get the length of a WMA file without using Windows Media Player?


I need to find a way to get the length of a WMA file without using any of the Windows Media Player(WMP) dlls. I found way to do it using WMP dlls, which you can see here, but due to another issue, I'd rather find a way where I don't have to use WMP.

我需要找到一种方法来获取WMA文件的长度,而无需使用任何Windows Media Player(WMP)dll。我找到了使用WMP dll的方法,你可以在这里看到,但由于另一个问题,我宁愿找到一种方法,我不必使用WMP。

One promising method that use NAudio and works with MP3s can be seen below:

使用NAudio并使用MP3的一种有前景的方法如下所示:

double GetMediaDuration(string MediaFilename)
{
    double duration = 0.0;
    using (FileStream fs = File.OpenRead(MediaFilename))
    {
        Mp3Frame frame = Mp3Frame.LoadFromStream(fs);
        if (frame != null)
        {
            _sampleFrequency = (uint)frame.SampleRate;
        }
        while (frame != null)
        {
            if (frame.ChannelMode == ChannelMode.Mono)
            {
                duration += (double)frame.SampleCount * 2.0 / (double)frame.SampleRate;
            }
            else
            {
                duration += (double)frame.SampleCount * 4.0 / (double)frame.SampleRate;
            }
            frame = Mp3Frame.LoadFromStream(fs);
        }
    }
    return duration;
}

Does anyone know of a way to port this over to work with a WMA file instead?

有没有人知道一种方法来移植它以使用WMA文件?

I looked at the source for the WindowsMediaFormat project, but I could not find a WMAFrame class or anything that might obviously let me swap out a class in order to get this working for WMAs.

我查看了WindowsMediaFormat项目的源代码,但我找不到WMAFrame类或任何可能显然让我换掉一个类的东西,以便让它适用于WMA。

2 个解决方案

#1


2  

You won't need NAudio for this one, but Windows Media.NET ( http://windowsmedianet.sourceforge.net/ )

你不需要NAudio,但Windows Media.NET(http://windowsmedianet.sourceforge.net/)

You will open MediaReader, and retrieve so called 'samples' that are compressed audio packages. Each of them should have its duration. Traverse the whole file, total the duration of each package, and here is your solution.

您将打开MediaReader,并检索作为压缩音频包的所谓“样本”。每个人都应该有自己的持续时间。遍历整个文件,总计每个包的持续时间,这是您的解决方案。

I'll get some code if I find time for it.

如果我找时间的话,我会得到一些代码。

More info:

更多信息:

Reader: http://msdn.microsoft.com/en-us/library/dd757425(v=vs.85).aspx

读者:http://msdn.microsoft.com/en-us/library/dd757425(v = vs。85).aspx

Callback for uncompressed samples: http://msdn.microsoft.com/en-us/library/dd743503(v=vs.85).aspx

未压缩样本的回调:http://msdn.microsoft.com/en-us/library/dd743503(v = vs。85).aspx

I hope that you'll be able to put it all together!

我希望你能把它们放在一起!

One problem though, and that is one that drives my friend nuts - regardless of how much actual PCM is compressed in WMA file, last compressed sample that you get will always be 'padded' to full frame length, so if you want actual length that was given before compression, you can forget it.

虽然有一个问题,那就是让我的朋友疯狂的问题 - 无论在WMA文件中压缩了多少实际的PCM,你得到的最后一个压缩样本将始终被“填充”到全帧长度,所以如果你想要实际长度那么在压缩之前给出,你可以忘记它。

#2


2  

You can use MediaFoundationReader and ask for its TotalDuration property. MediaFoundationReader is new to NAudio 1.7 and available in a preview release on NuGet. The length is calculated using the MF_PD_DURATION Media Foundation presentation attribute.

您可以使用MediaFoundationReader并询问其TotalDuration属性。 MediaFoundationReader是NAudio 1.7的新手,可在NuGet的预览版中使用。使用MF_PD_DURATION Media Foundation演示文稿属性计算长度。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/04/19/208a7f7236902f2dc262bce892e56f87.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告