Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mp3

I need to create a M3U playlist of mp3 songs.

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

like image 237
BIKTOP Avatar asked Oct 21 '25 06:10

BIKTOP


2 Answers

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.

like image 141
Fᴀʀʜᴀɴ Aɴᴀᴍ Avatar answered Oct 23 '25 20:10

Fᴀʀʜᴀɴ Aɴᴀᴍ


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;
like image 33
BIKTOP Avatar answered Oct 23 '25 20:10

BIKTOP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!