[edit]
This is the working code after Hans Passant's comments which helped me to compile it and test it. This code compiles and displays the length of the movie.
using System;
using WMPLib;
namespace MediaPlayer
{
class Program
{
static WindowsMediaPlayer wmp = new WindowsMediaPlayer();
static void Main(string[] args)
{
wmp.URL = @"c:\Wildlife.wmv";
wmp.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlayStateChange);
Console.ReadKey();
}
static void wmp_PlayStateChange(int NewState)
{
if (NewState == 3)
{
Console.WriteLine("Duration = " + wmp.currentMedia.durationString);
}
}
}
}
[old answer]
I don't know anything about this stuff, but here is my take on it. The state of the player is not where it can report on the media yet. The code below is just thrown together on here and might not even compile.
From MSDN:
To retrieve the duration for files that are not in the user's library, you must wait for Windows Media Player to open the file; that is, the current OpenState must equal MediaOpen. You can verify this by handling the Player.OpenStateChange event or by periodically checking the value of Player.openState.
using WMPLib;
class Program
{
static void Main(string[] args)
{
// this file is called Interop.WMPLib.dll
WindowsMediaPlayer wmp = new WindowsMediaPlayer();
wmp.URL = @"c:\TORRENT.KG\Assault.girls.2009.DVDRip.Rus.Eng.avi";
wmp.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlayStateChange);
Console.ReadKey();
}
void wmp_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 3)
{
Console.WriteLine("Duration = " + wmp.currentMedia.durationString);
}
}
}