Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Your Own MP3 Duration Calculator

Gigi Sayfan specializes in cross-platform object-oriented programming in C/C++/ C#/Python/Java with emphasis on large-scale distributed systems. He is currently trying to build intelligent machines inspired by the brain at Numenta (www.numenta.com).

My wife recently completed certification as a spinning instructor -- you know, riding real hard on a stationary bike -- and began teaching classes. It turns out that music is very important for spinning, and instructors have to prepare CDs with proper tracks for warm-up, main work-out, and cool down. Each part is measured and you have to find the right tracks with the proper combined length. Of course, you could do this manually by looking at the duration of each track and try to add them together, but this approach is tedious and error-prone. A more serious problem is that the duration of MP3 files is recorded as an ID3 tag in the file header. There is no guarantee that this number is correct (and often it is not). This situation is simply screaming for a software solution.

The solution I present in this article -- the "MP3 Duration Calculator" -- lets you select MP3 files from a directory and calculate the total duration of the selected files on-the-fly. The complete source code and related files are available here. To calculate the duration of each track, the software actually opens the file in a media player and gets an accurate duration number. Figure 1 shows the folder selection dialog where you choose the folder that contains the MP3 files.

Figure 1

Figure 2 shows the main window of the MP3 Duration Converter.

Figure 2

The program is a WPF application implemented in C# over the .NET 3.5 framework. It demonstrates a few interesting aspects such as:

  • Dynamically loading MP3 files
  • Working with the MediaElement component
  • Asynchronous execution of long operations while keeping the UI responsive
  • On-the-fly resizing of GridView columns (more obscure then it should be)
  • Data binding and custom conversion
  • Application state persistence

An Introduction to WPF

WPF (Windows Presentation Foundation) is the user interface/display system of Windows. It is built on top of DirectX and can take advantage of hardware acceleration. It is more advanced than previous UI APIs (including the .NET Windows Forms) all based on GDI/GDI+ and the User32 API. WPF offers a declarative UI (XAML), animation, multi-media support, rich text model, web-like layout model, browser-like page-based navigation, rich drawing model (including 3D) based on primitives and not pixels, totally customizable look-and-feel for controls (via templates and styling), and more.

Silverlight, Microsoft's rich web client platform, is based on the same design and is almost compatible, which really helps if you want to multi-target your application to the desktop and the browser. At this writing, WPF is not supported by Mono (and probably won't in the near future), so if you need to target multiple platforms your best bet is still the Windows Forms API.


XAML, short for "eXtensible Application Markup Language," is an XML dialect for instantiating .NET objects and hooking them up. It can be used for many purposes, but currently its primary use is to define WPF user interfaces. It provides the following capabilities:

  • Full namespace support
  • Resource definition
  • Animation definition
  • Control template definition
  • Hooking up event handlers
  • Data binding
  • Design time integration with Visual Studio 2008

This is a rich feature set for a UI description markup language. It should be noted that XAML is not required and you can code any WPF construct programmatically. However, many things are easier with XAML, especially if you use Visual Studio as your IDE.

MP3 Duration Calculator Design

The design is straightforward. There are four classes:

  • App (the application class)
  • TrackDurations (engine)
  • DurationConverter (a helper class)
  • MainWindow (GUI).

The App and MainWindow classes are a code-behind classes for the corresponding XAML files: App.xaml and MainWindow.xaml.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.