0

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:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;
Jeffrey Sax
  • 10,253
  • 3
  • 29
  • 40
  • 1
    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? – Jamie Keeling Oct 25 '11 at 19:13
  • Post the code where you assign file (or filename) to wplayer. And more: what's wplayer? Show us your code please... – Marco Oct 25 '11 at 19:15
  • 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 – Whatisit Nameo Oct 25 '11 at 19:15
  • WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); its windows media player – Whatisit Nameo Oct 25 '11 at 19:16
  • 1
    Maybe, You should wait a little bit before `wplayer.currentMedia.durationString`. In debug, Player has time to start. – L.B Oct 25 '11 at 19:18
  • Thanks L.B, That delay did it!! – Whatisit Nameo Oct 25 '11 at 20:00

3 Answers3

0

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

  • 1
    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. – Bruce Jun 08 '16 at 07:42
0

I find that the method that calls player.controls.play() 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:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;

This also will not work:

wplayer.controls.play();
Application.DoEvents();
System.Threading.Thread.Sleep(100000);
Application.DoEvents();
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 
AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);

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;
wplayer.controls.play();
return;

// 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();
}
Dan
  • 99
  • 5
0

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:

player.controls.currentPosition

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.

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102