views:

220

answers:

1

Hi guys,

I am just starting with DirectShow.NET, and I am trying to get the length (in seconds) of an audio file. The audio may be .mp3, .wav, .aac, or .m4a.

Can I get this information using DirectShow, or do I need some other APIs?

+1  A: 

yes you can do this with DirectShow. There are a variety of ways to do this. One way is to query the IMediaSeeking interface on the graph object, and then call the GetDuration method on this interface.

GetDuration returns a 64bit integer value for how long it would take to play the file.

You will need to call the GetTimeFormat method to find out what units the duration is in. The most likely default value is TIME_FORMAT_MEDIA_TIME which is 10ths of a microsecond.

IN that case you would divide the duration by 10*1000*1000 to get seconds.

You can also call SetTimeFormat before calling GetDuration if you want to force the units.

John Knoeller
I'll give that a try. Thank you.
Judah Himango
I tried this, and at first it didn't work. If I called graph.RenderFile first, then called GetDuraction, it worked. Thanks for the help!
Judah Himango
@Judah: Yes, the graph has to know which file before it can tell you how long.
John Knoeller
In retrospect that makes sense, but it didn't occur to me I needed to call RenderFile first, as we were already dealing with this particular file elsewhere in the DShow APIs. Anyways, thanks for the help.
Judah Himango
@Judah: no problem. Its hard to know what you already know when we aren't actually looking at code.
John Knoeller