title: How to retrieve the correct Duration of an mp3 audio file in C#/.NET/DirectX?
body: <p>I am writing a media presentation program, and I am nearly complete, but for some reason I am getting incorrect durations of mp3 files. I use the Microsoft.DirectX.AudioVideoPlayback.Audio class to play audio files, and it works well for wav files. From google search, I can tell that it seems to be a problem when the mp3s are encoded using a variable bit rate, however I need my program to report accurately regardless of the bit rate settings of the media. Here is the code:</p>
  
  <pre><code>Media_Audio aud = (Media_Audio)item.MediaFile;
  TimeSpan total = aud.EndTime - aud.StartTime;
  if (aud.EndTime.TotalSeconds == 0)
  {
      total = TimeSpan.FromSeconds(aud.Player.Duration)-aud.StartTime;
  }
  TimeSpan now = TimeSpan.FromSeconds(aud.Player.CurrentPosition) - aud.StartTime;
  string nows = now.Hours.ToString() + ":" + now.Minutes.ToString() + ":" + now.Seconds.ToString();
  string tots = total.Hours.ToString() + ":" + total.Minutes.ToString() + ":" + total.Seconds.ToString();
  item.currentPosition.Text = nows + "/" + tots;
  </code></pre>
  
  <p>The key line being:</p>
  
  <pre><code>total = TimeSpan.FromSeconds(aud.Player.Duration)-aud.StartTime;
  </code></pre>
  
  <p>aud.Player is an instance of Microsoft.DirectX.AudioVideoPlayback.Audio.</p>
  
  <p>For a song that is, in fact, 5:49 seconds long, it reports 39:13 as the duration. The same length as Windows Explorer and VLC Player. Also, have the K-Lite Mega Codec pack installed. so it could be interfering. I am aware of this question: <a href="https://stackoverflow.com/questions/383164/how-to-retrieve-duration-of-mp3-in-net">How to retrieve duration of MP3 in .NET?</a>
  However, at this stage in my program I do not have the time to switch from my current method of playing audio files to a new one, so do I have any other options to determine the duration using DirectX only? Or Will I have to resort to BASS.NET or TagLib Sharp? (And does meta-data even contain accurate information for VBR-encoded mp3s?)</p>