karthik
karthik

Reputation:

calculate playing time of a .mp3 file

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?

Upvotes: 2

Views: 4756

Answers (4)

Alex
Alex

Reputation: 133

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

Upvotes: 3

Jani Hartikainen
Jani Hartikainen

Reputation: 43243

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.

Upvotes: 0

Merus
Merus

Reputation: 8984

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

Upvotes: 2

marcc
marcc

Reputation: 12399

You could use TagLib Sharp

It exposes TagLib.AudioProperties.Duration

Upvotes: 3

Related Questions