locked
Getting an track length with Windows Media Player

    Question

  • How can I get the length of an track with Windows Media Player (WMPLib/axWindowsMediaPlayer)?

     

    Thanks in advance.

    Saturday, February 16, 2008 8:54 AM

Answers

  • Look at this example:

     

     

    using System;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            bool loadingPlayer;
            public Form1()
            {
                InitializeComponent();
                loadingPlayer = false;
                myPlayer.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(myPlayer_PlayStateChange);           
            }
     
            //Get duration if first played.  Display in a label
            void myPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
            {
                if (loadingPlayer && e.newState == 3)
                {
                    label1.Text = myPlayer.currentMedia.durationString;
                    progressBar1.Maximum = (int)myPlayer.currentMedia.duration;
                    loadingPlayer = false;
                    myPlayer.Ctlcontrols.stop();
                }
            }
     
            //Load player from file and start playing
            private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    myPlayer.URL = openFileDialog1.FileName;
                    loadingPlayer = true;
                    myPlayer.Ctlcontrols.play();
                }
            }
     
        }
    }
    Saturday, February 16, 2008 3:05 PM

All replies

  • You can get the duration of the current track as a double or a string like this:

     

    myPlayer.currentMedia.durationString;

    myPlayer.currentMedia.duration;

     

    The track has to be loaded and started before this information is available, however.  When I load the track, I immediately start play and use an event to capture the media player's state change (playing).  In the event handler, I stop the player and grab the duration values. 

     

    Let me know if you need more details.

     

    Saturday, February 16, 2008 11:40 AM
  •  

    This is my open file script so you can let it work for me Wink

    Code Snippet

    openFileDialog1.ShowDialog();

    if (openFileDialog1.FileName == "")

    {

    // we zijn voorgelogen! we hoeven niks te openen.

    }

    else

    {

    // openen maar

    WMPLib.IWMPControls3 controls = (WMPLib.IWMPControls3)axWindowsMediaPlayer1.Ctlcontrols;

    axWindowsMediaPlayer1.URL = openFileDialog1.FileName;

    controls.play();

    label2.Text = "Length: " + axWindowsMediaPlayer1.currentMedia.durationString;

    controls.stop();

     

     

    Thanks in advance.
    Saturday, February 16, 2008 2:57 PM
  • Look at this example:

     

     

    using System;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            bool loadingPlayer;
            public Form1()
            {
                InitializeComponent();
                loadingPlayer = false;
                myPlayer.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(myPlayer_PlayStateChange);           
            }
     
            //Get duration if first played.  Display in a label
            void myPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
            {
                if (loadingPlayer && e.newState == 3)
                {
                    label1.Text = myPlayer.currentMedia.durationString;
                    progressBar1.Maximum = (int)myPlayer.currentMedia.duration;
                    loadingPlayer = false;
                    myPlayer.Ctlcontrols.stop();
                }
            }
     
            //Load player from file and start playing
            private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    myPlayer.URL = openFileDialog1.FileName;
                    loadingPlayer = true;
                    myPlayer.Ctlcontrols.play();
                }
            }
     
        }
    }
    Saturday, February 16, 2008 3:05 PM