ChrisCreateBoss
ChrisCreateBoss

Reputation: 323

How to format a song's duration time into minutes and seconds?

I am working on a Windows Form Application in which I have a Music Player using the "axWindowsMediaPlayer" component. It can import mp3 and wav files, display the songs in a listbox, save and load a playlist, etc.

Now I want a label to show the elapsed time of the song that is currently playing in the minutes:seconds format.

How would I do that?

Here's one of my tries:

int count = (int)axWindowsMediaPlayer.currentMedia.duration;
count--;
elapsedLbl.Text = count.ToString();

Here's where I'm stuck.

[EDIT] In this moment I have formatted it correctly, but now the count isn't going down.

Upvotes: 1

Views: 2999

Answers (3)

Edward Eisenhart
Edward Eisenhart

Reputation: 390

If you use the Windows Media Player ActiveX control there is a duration and a currentPosition property.

AxWindowsMediaPlayer1.currentMedia.duration

AxWindowsMediaPlayer1.Ctlcontrols.currentPosition

Both are in seconds so then you just have to convert them to the desired format. You can use:

TimeSpan.FromSeconds(double value)

To create a timespan and then with that timespan convert it to a formatted string as YoupTube mentioned. Or manually convert the seconds into minutes and seconds as Mark Shevchenko mentioned.

Upvotes: 3

Youp Bernoulli
Youp Bernoulli

Reputation: 5655

Something like:

timespanVariable.ToString("mm:\\ss")

Or when you have a DateTime:

datetimeVariable.ToString("mm:ss")

As Matt Johnson mentioned, here's a link to it.

Upvotes: 1

Steve Mitcham
Steve Mitcham

Reputation: 5313

The AxWindowsMediaPlayer object can retrieve the current media item through a currentMedia property. There is a property on this object of durationString that returns 'HH:MM:SS' format. Other formats are possible by using the duration object.

AxWindowsMediaPlayer docs http://msdn.microsoft.com/en-us/library/windows/desktop/dd562405(v=vs.85).aspx

IWMPMedia interface docs http://msdn.microsoft.com/en-us/library/windows/desktop/dd563397%28v=vs.85%29.aspx

Upvotes: 0

Related Questions