【问题标题】:I'm trying to read a video duration from file, but it give just zeros. Help me [closed]我正在尝试从文件中读取视频持续时间,但它只给出零。帮帮我[关闭]
【发布时间】:2010-12-10 18:18:35
【问题描述】:

这段代码 sn-p 不起作用

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

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

【问题讨论】:

  • Kojomuratov 小心提问)
  • @STW:也许,可能,这是他供个人使用的档案副本。 :)
  • @Joe aha Assault.girls.2009...(:
  • 这是 Windows 7 附带的示例视频。但是文件夹错误。

标签: c# video duration


【解决方案1】:

[编辑]

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

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

【讨论】:

  • @Pieter 谢谢我一直在寻找类似的东西顺便提一下stackoverflow.com/questions/4411519/…的同一篇文章
  • 你真的需要在发布之前编译这个。您正在将 Windows 窗体的内容混合到控制台应用程序中。
  • @Hans 我没有 SDK,也找不到下载它,所以我想我会给他一些东西而不是什么,但是是的,你是对的,我应该正确地完成它。
  • 不需要SDK,只需添加引用,浏览选项卡,选择c:\windows\system32\wmp.dll
  • @Hans 谢谢!我用可以编译和工作的代码更新了答案。
猜你喜欢
  • 2022-10-09
  • 2018-06-30
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2021-07-15
  • 2014-11-20
相关资源
最近更新 更多