2

I'm doing a sample which will run mp3 files which are selected by the user. I want to calculate the playing time of the file (e.g. 00:05:32). How can I calculate the playing time?

tux3
  • 7,171
  • 6
  • 39
  • 51

4 Answers4

3

You could use TagLib Sharp

It exposes TagLib.AudioProperties.Duration

marcc
  • 12,295
  • 7
  • 49
  • 59
3

For Alvas.Audio library see code below

    using (Mp3Reader mr = new Mp3Reader(File.OpenRead("len.mp3")))
    {
        int durationMS = mr.GetDurationInMS();
        TimeSpan durationTS = TimeSpan.FromMilliseconds(durationMS);
    }
Alex
  • 133
  • 2
  • 4
2

You suggest in the tag that you're doing this in C#. This question deals with it:

Finding MP3 length in C#

And there's some code for reading the MP3 header and extracting relevant information (like the length) here:

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=79

Community
  • 1
  • 1
Merus
  • 8,796
  • 5
  • 28
  • 41
  • 2
    the approach on devhood is definitely wrong because it uses the file length to calculate the duration. this is an incorrect since for example adding an album art to the mp3 file increases the file length, but it does not effect the duration. this method fails at this point and i would not recommend it. – aslı Mar 21 '10 at 19:44
0

I believe the Windows Media API (or windows mixer api or something, I can't recall the exact name) has a way to open and read sound files like mp3 and maybe get the time from it too. As an added bonus, by using that API you can open any audio format that will work in say Windows Media Player, so you're not limited to just mp3's.

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86