tags:

views:

355

answers:

3

Dupe of calculate playing time of a .mp3 file

im reading a audio file(for ex:wav,mp3 etc) and get a long value as duration.now i want to convert that long value into correct time duration(like,00:05:32)

+3  A: 

Depending on exactly what the long represents, you could probably use one of the TimeSpan constructor overloads to get a TimeSpan object representing the duration of the sound file.

Assuming the long represents milliseconds:

long soundLength = GetSoundLength();
TimeSpan duration = new TimeSpan(0, 0, 0, 0, soundLength);
Console.WriteLine("{0} minutes and {1} seconds", duration.Minutes, duration.Seconds);

Edit: fixed the contstructor call; it was one parameter short.

Fredrik Mörk
+1  A: 

First you should determine what it is that you are receiving as input. Then you can convert this into a TimeSpan object which is easy to work with and display onscreen. See http://msdn.microsoft.com/en-us/library/system.timespan.aspx for more information.

Peter
A: 

taglib-sharp gives you the mp3 duration exactly as you like.

and it's this easy to call

TagLib.File mp3file = TagLib.File.Create("exactFilePath");
Console.WriteLine(mp3file.Properties.Duration);
davsan