我正在尝试从文件中读取视频持续时间,但它只给出零。帮我

时间:2010-12-10 18:18:36

标签: c# video duration

此代码段不起作用

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

它只给我零。有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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);
        }
    }
}