'How to get a duration of mp3 file via .NET/C#? [closed]

I need to create a M3U playlist of mp3 songs.

Is there any way to read the duration attribute of MP3 files?



Solution 1:[1]

Don't reinvent the wheel. Use an external library such as NAudio to do the hard work.

  • NAudio is available at GitHub.

You can use it like this:

Mp3FileReader reader = new Mp3FileReader("<YourMP3>.mp3");
TimeSpan duration = reader.TotalTime;

Of course, an alternative would be this answer.

To use the Mp3FileReader class, you must add using NAudio.Wave; to your file.

Solution 2:[2]

Thanks everyone for help.

I tryed using TagLib and it works fine.

TagLib.File f = TagLib.File.Create(<pathToFile>, TagLib.ReadStyle.Average);
var duration = (int)f.Properties.Duration.TotalSeconds;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zChris128
Solution 2 BIKTOP