-8

this code snippet doesn't work

using WMPLib;
class Program
{
    static void Main(string[] args)
    {
        // this file is called Interop.WMPLib.dll
        WindowsMediaPlayer wmp = new WindowsMediaPlayer();
        wmp.URL = @"c:\Wildlife.wmv";
        Console.WriteLine("Duration = " + wmp.currentMedia.durationString);
        // write named attributes
        Console.ReadKey();
    }
}

It just give me zeros. Can anyone help?

dastanko
  • 125
  • 1
  • 10

1 Answers1

4

[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);
        }
    }
}
Pieter
  • 2,188
  • 20
  • 27
  • @Pieter thanks I've been looking for something like this by the way the same post at http://stackoverflow.com/questions/4411519/c-how-to-read-things-like-id3-tag-for-a-video-files – sultan Dec 10 '10 at 18:39
  • You really need to compile this before posting. You are mixing Windows Forms stuff into a console app. – Hans Passant Dec 10 '10 at 20:27
  • @Hans I do not have the SDK and could not find it to download it so I thought I'd give him something rather than nothing, but yes, you are right I should have done it properly. – Pieter Dec 11 '10 at 17:29
  • No need for the SDK, just Add Reference, Browse tab, select c:\windows\system32\wmp.dll – Hans Passant Dec 11 '10 at 17:35
  • @Hans Thanks! I updated the answer with code that compiles and works. – Pieter Dec 11 '10 at 22:39
  • Awesome, +1 for a great answer. – Hans Passant Dec 11 '10 at 23:09