问题描述
我需要找到一种方式来获得一个WMA文件的长度,而无需使用任何Windows媒体播放器(WMP)的DLL。我发现了一种使用WMP的dll,你可以在这里看到 做到这一点,但由于另一个问题,我宁可找到一种方法,我没有使用WMP。
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.
一个有希望的方法,该方法使用 n音讯,并适用于MP3的下面可以看到:
One promising method that use NAudio and works with MP3s can be seen below:
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;
}
有谁知道的一种方式来港结束这一个WMA文件,而不是工作?
Does anyone know of a way to port this over to work with a WMA file instead?
我看了看源WindowsMediaFormat项目,但我找不到WMAFrame类或任何可能很明显,让我换出一类为了得到这个工作的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.
推荐答案
您将不再需要n音讯这一个,但Windows Media.NET(的)
You won't need NAudio for this one, but Windows Media.NET ( http://windowsmedianet.sourceforge.net/ )
您将打开MediaReader和检索所谓的样品是COM pressed音频包。他们每个人都应该有它的持续时间。遍历整个文件,每个总包的时间,这里是您的解决方案。
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.
我会得到一些code,如果我找个时间吧。
I'll get some code if I find time for it.
更多信息:
读者: http://msdn.microsoft .COM / EN-US /库/ dd757425(V = vs.85)的.aspx
回调uncom pressed样本: http://msdn.microsoft.com/en-us/library/dd743503(v=vs.85).aspx
Callback for uncompressed samples: http://msdn.microsoft.com/en-us/library/dd743503(v=vs.85).aspx
我希望你能够把它放在一起!
I hope that you'll be able to put it all together!
不过有一个问题,那就是一个驱动我的朋友的坚果 - 你得到的将永远是填充,而不管其COM $ P $多少实际PCM pssed的WMA文件,pssed最后COM $ P $样品全帧的长度,所以如果你想那是以前的COM pression给出实际长度,你可以忘掉它。
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文件的长度,而无需使用Windows媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!