We help IT Professionals succeed at work.

Get Length of Audio File

Medium Priority
137 Views
Last Modified: 2019-03-22
How do you open an audio file (mp3, wav, etc.) and get the length of the file in seconds using WMPLib.WindowsMediaPlayer with Visual Basic 2015?
Comment
Watch Question

Analyst developer
CERTIFIED EXPERT
Commented:
Hi Ray Turner,

You can try this code that i found in this page:

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

Open in new window

Dr. KlahnPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
Side note:  If you're using MP3 files encoded VBR, check several of them to be sure that the results match the actual play times.  Multiplying the number of frames times the first frame bitrate (the simple method) does not work with VBR files.
Ray TurnerSenior Consultant

Author

Commented:
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.