-8

此代码段不起作用

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

它只是给我零。任何人都可以帮忙吗?

4

1 回答 1

4

[编辑]

这是 Hans Passant 评论后的工作代码,它帮助我编译和测试它。此代码编译并显示电影的长度。

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

[老答案]

我对这些东西一无所知,但这是我的看法。播放器的状态还不是它可以在媒体上报道的地方。下面的代码只是在这里拼凑在一起,甚至可能无法编译。来自 MSDN:

要检索不在用户库中的文件的持续时间,您必须等待 Windows Media Player 打开文件;也就是说,当前的 OpenState 必须等于 MediaOpen。您可以通过处理 Player.OpenStateChange 事件或定期检查 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);
        }
    }
}
于 2010-12-10T18:34:00.260 回答