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();
}