Possible to get the length of a WMA file without using Windows Media Player?
Asked Answered
S

2

1

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.

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

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

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.

Spue answered 19/4, 2013 at 15:8 Comment(0)
V
2

You won't need NAudio for this one, but 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.

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

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!

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.

Vining answered 19/4, 2013 at 15:39 Comment(6)
I was reading through the documentation and I came across this line: but everything else in the library is just definitions to allow access to the code in Microsoft’s Windows Media DLLs. Doesn't this seem to suggest that it will be using the Windows Media Player dlls?Spue
Sure you will, but will not use media player as such. You want something without even touching winmp.dll?Deweydewhirst
wmp.dll is what I have been using and it has been a nightmare to get it to work on a 64 bit server.Spue
Did you try enabling Windows Experience? I mean, I work on Win2008R2 with windows media player all the time.Deweydewhirst
No, I haven't even heard of it. This is running on windows server 2003 also. not sure if that makes a difference.Spue
social.msdn.microsoft.com/Forums/en-US/…Deweydewhirst
C
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.

Conformist answered 20/4, 2013 at 7:45 Comment(4)
Any chance you know where I could download mfplatt.dll? When I try to instantiate MediaFoundationReader it throws an error saying it can't find that particular dll.Spue
what version of Windows are you using?Conformist
may be too early for Media Foundation. You might still be able to use the WmaFileReader in NAudio's Windows Media libraryConformist
Instantiating that gives me the error: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) which I think is related to the fact that I'm running a 64 bit application. Unfortunately I've been chasing my tail on the 32bit/64bit issue for about a week now.Spue

© 2022 - 2024 — McMap. All rights reserved.