In the uncompressed situation I know I need to read the wav header, pull out the number of channels, bits, and sample rate and work it out from there: (channels) * (bits) * (samples/s) * (seconds) = (filesize)
Is there a simpler way - a free library, or something in the .net framework perhaps?
How would I do this if the .wav file is compressed (with the mpeg codec for example)?
Yes, There is a free library that can be used to get time duration of Audio file. This library also provides many more functionalities.
TagLib
TagLib is distributed under the GNU Lesser General Public License (LGPL) and Mozilla Public License (MPL).
I implemented below code that returns time duration in seconds.
You may consider using the mciSendString(...) function (error checking is omitted for clarity):
I'm gonna have to say MediaInfo, I have been using it for over a year with a audio/video encoding application I'm working on. It gives all the information for wav files along with almost every other format.
MediaInfoDll Comes with sample C# code on how to get it working.