tags:

views:

761

answers:

4

I have build a WPF application where users can drag and drop MP3 files onto a listbox. I need a way to calculate the total duration of the playlist.

Any libraries I should use? Or is it possible using only the .NET framework?

+3  A: 

Have a look at

I have used both with some success.

flesh
Hmm looks interesting, will give it a try and see if it answers my requirements. Thanks!
Sander Versluys
+1  A: 

I ended up writing my own for this purpose a few years back. You can write an effective one client-side using flash. The key points that I remember are:

  1. that you can't rely on the MP3 header to be accurate, so don't just read that to get the duration
  2. in flash, seek to the end of the MP3, and then read the current position. That will give you the duration.

Edit: I realize you wanted to do this with .Net, but it may actually be more useful to know client side before you upload the file to the server, as if you wish to impose limits on file length, you can do that much earlier in the process.

RedFilter
The tip for flash will come in handy in the future as i have had to deal with it in the past and I indeed encountered wrong duration readings which would mess up the scrubber. Thanks!
Sander Versluys
+1  A: 

[my own solution]

I've added a second mediaelement control which I use to open each mp3 file in the listbox and get it's NaturalDuration. Open and close each file in the list and make a sum of all the values.

As stated by others, the mp3 headers aren't accurate. The mediaelement provides the correct duration.

It's probably not the fastest method but in my case the simplest (it does not rely on other libraries).

Sander Versluys
+1  A: 

I wrapped mp3 decoder library and made it available for .net developers. You can find it here:

http://sourceforge.net/projects/mpg123net/

Included are the samples to convert mp3 file to PCM, and read ID3 tags.

I guess that it can be used to properly calculate mp3 file duration.

Daniel Mošmondor