使用WMPLib从视频文件.wmv获取currentPositionTimecode

时间:2014-06-03 14:58:54

标签: c# windows-media-player

我想从.wmv文件中提取第一个时间码。目前我正在使用这段代码:

        var wmp = new WindowsMediaPlayer();
        wmp.newMedia(filename);
        WMPLib.IWMPControls3 controls = (WMPLib.IWMPControls3)wmp.controls;
        Thread.Sleep(5000);
        Console.WriteLine(controls.currentPosition);
        Console.WriteLine(controls.currentPositionTimecode);

输出始终为0且00:00:00.00我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

最后我发现了如何做到这一点:

using WMPLib;

public partial class MainWindow : Window
{
    private string _Timecode;
     WindowsMediaPlayerClass _Wmp =  new WindowsMediaPlayerClass{volume = 0};
    public MainWindow()
    {
        InitializeComponent();
        _Wmp.MediaChange += WmpOnMediaChange;
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(_Timecode);
    }

    private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
    {
        const string filename = @"C:\Users\Public\Videos\Toto.wmv";
        _Wmp.URL = filename;
    }

    private void WmpOnMediaChange(object item)
    {
        _Wmp.MediaChange -= WmpOnMediaChange;
        _Wmp.controls.pause();
        _Wmp.controls.currentPosition = 0 ;
        _Timecode = ((IWMPControls3) _Wmp.controls).currentPositionTimecode;
        _Wmp.close();
        _Wmp = null;
    }
}