Programmatically getting track length of a song from Windows Media Player

Related searches

I want to get the track length of a song. When i debug it, i was able to get the track length and set it into a string variable called songDuration. However when i actually execute it, it does not work, the string is null. how do i make it work? I have tried initializing the variables first but still same result. What am i missing? It works when i debug it line by line, just not running it.

Heres the piece of code:;
songDuration = wplayer.currentMedia.durationString;

This should be the property you are looking for, it accepts a double so you will need to take into consideration how you can use the songDuration string variable you have at the moment:


This MSDN article should give you some more information, although the examples points towards using the control in a web page I believe the content should be similar.

Additionally this SO question appears to be similar but contains an answer already.

Getting an track length with Windows Media Player - MSDN, Visual C# Express Edition How can I get the length of an track with Windows Media Player //Load player from file and start playing Song length editing on Windows media player I'm trying to edit the length of a song by 43 seconds. can take from the beginning and the end. Windows 10 does not

label3.Text = (wmp.Ctlcontrols.currentPosition ).ToString(); label4.Text = wmp.Ctlcontrols.currentItem.duration.ToString();

IWMPMedia duration property, For playlists, the duration of each media item can be retrieved when EventArgs e) { // Subtract the current position from the duration of the current media to get // the time remaining. Windows Media Player 9 Series or later. 1 Start up Windows Media Player and open the "Library." 2 Right-click on a song in your library and select "Edit." 3 Choose what field you want to edit, make your edit and then press the "Enter" key on your keyboard. You can edit the title, length and genre of your song, as well as the album, contributing artist and release year.

I find that the method that calls must exit and the player must actually start playing for a fraction of a second before the .duration and .durationString properties will return correct values and not an empty string and 0.

This will not work:;
songDuration = wplayer.currentMedia.durationString;

This also will not work:;
songDuration = wplayer.currentMedia.durationString;

I solved it by starting to play the media and exiting the method, creating a Timer event that triggers every 100 msec and each time it is called, it checks if duration is still 0, when it is not, it can capture the duration and pause the media. Another approach is to use AxWindowsMediaPlayer where you can add an event handler when the media state changes and when it starts playing, you can trap that event and see the duration. I did not go that route and did not want to take the steps to import whatever namespace that was using. That said, this is what MSFT suggests:

// Add a delegate for the PlayStateChange event.
player.PlayStateChange += new 

Instead of installing the SDK, I went about it this way. Start the player like this

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += new EventHandler(GetDuration);
myTimer.Interval = 100;;

// Check for duration in this other routine which runs every 100 msec until 
// Media Player tells us the duration.

private string GetDuration()
    // public variable songDuration declared elsewhere
    songDuration = wplayer.currentMedia.durationString;
    if (songDuration.Length > 0) wplayer.controls.pause();

Calculate the duration of a song (Media Player), The code below calculates the length of the track in minutes and seconds from the Windows Media Player properties (which just gives the result� Unlike Media Player 11, Windows Media Player 12 no longer offers an advanced editor to allow you to update and correct song tags. Instead, the player edits them for you automatically from an online database. That’s all well and good when it gets it right, but there are many times when Windows Media Player 12 …

How to Get Media File Duration in C#, If you've ever needed to get hold of the duration of a media file such as an My preference is to get the value from the Windows Shell, and the the I needed to track the no.of hours of video lectures I learn per day. I'd expect in UWP the easiest way is to load it into a mediaplayer and ask for duration. I am using Windows Media Player ActiveX (axWindowsMediaPlayer) component in C# WinForms application. Is it possible to get and set the current position of the video that is being played using this

Windows Media Player, Windows Media Player (WMP) is a media player and media library application developed by While playing music, Windows Media Player can show visualizations. parental controls and audio track language selection if compatible decoders Windows Mobile, classic Mac OS, Mac OS X, Palm-size PC, Handheld PC,� 0 Programmatically getting track length of a song from Windows Media Player Mar 10 '19. Badges (3) Gold

Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

  • Can you post the code where you're actually assigning the string a value please. Also have you checked the documentation for the objects you are tryng to use?
  • Post the code where you assign file (or filename) to wplayer. And more: what's wplayer? Show us your code please...
  • string songDuration=""; Thats how i iniitalze my string to a blank, then after i play the track, i set songDuration to the track duration as describe above, but only able to get the duration if i debug line by line, otherwise it will be null when i run it
  • WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); its windows media player
  • Maybe, You should wait a little bit before wplayer.currentMedia.durationString. In debug, Player has time to start.
  • I dont want the current position, i want to know how long the song is
  • I got it solve, L.B was right, it was going too fast, it was trying to get the length of the song before the song is actually started, I put a second delay before it.
  • Oops, noticed you edited the question. Now I actually understand what you mean ;)
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.