Search code examples
c#windows-media-player

Get currentPositionTimecode from a video file .wmv using WMPLib


I would like to extract the first timecode from a .wmv file. Currently i'm using this piece of code :

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

The output is always 0 and [00000]00:00:00.00 and I don't know what am I doing wrong.


Solution

  • Finally I found how to do it :

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