getting length of  a  wav file

Posted on 1998-05-31
Last Modified: 2010-08-05
I know it is possible to get the length of a wav file (in milleseconds or seconds) but have not been able to do it.  Below is a function that is supposed to return the length of a wave file (IT DOESN'T WORK... IT RETURNS 0.0 EVERY TIME).  The code uses MCI_WAVE_OPEN_PARMS and mciSendCommand() to open a file (which it does with out error) but the dwBufferSeconds member of MCI_WAVE_OPEN_PARMS is always 0.0 after opening the wav.  Am I wrong in assuming that dwBufferSeconds should hold the length of the wav file opened or am I missing a needed flag or something?  Does somebody know what I am doing wrong or know of another way to get the length of a wav file?  Thanks.

// Function takes LPSTR pointer to string containing the
// path to a wave file, returns length of wave file
long CPlaywaveDlg::GetWavLengthSeconds(LPSTR wavPath)
{       // wavPath points to string containing wave file path
      MCI_WAVE_OPEN_PARMS mciOpenParms;
      DWORD mciError, mciFlags;
      int wDeviceID;
#define BUF_LEN 256
      char mciErrorString[BUF_LEN];

      //Initialize data structures...
      mciOpenParms.dwCallback = 0;  // no callback
      mciOpenParms.wDeviceID = 0;  
      mciOpenParms.lpstrDeviceType = "waveaudio";
      mciOpenParms.lpstrElementName = wavPath;
      mciOpenParms.lpstrAlias= NULL;       // no alias
      mciOpenParms.dwBufferSeconds = 0L;  

      // Send the open command
      mciError = mciSendCommand(0, MCI_OPEN, mciFlags, (DWORD)&mciOpenParms);
      if (!mciError)

            // if open was successful, retrieve the length
            double secs = mciOpenParms.dwBufferSeconds;
            mciError = mciSendCommand(wDeviceID, MCI_CLOSE, 0, 0L); // close the file

            return secs;  // return the length

            int dummy = mciGetErrorString(mciError, mciErrorString, BUF_LEN);
            CString errorMsg = "Media Control Interface Error ";
            errorMsg += mciErrorString;
            MessageBox(errorMsg, "Error", MB_ICONSTOP);
      return -999;  // error


Question by:cww1

Author Comment

ID: 1164999
Edited text of question

Accepted Solution

tma050898 earned 100 total points
ID: 1165000

I only have a minute to post this so I'm tryig to copy & paste this out of my code very quickly. Try this and if it doesn't work I'll take a little more time when I get back home...

// Get Minutes...
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms);

iMinutes = ((int)((StatusParms.dwReturn / 1000) / 60 ));

// Get seconds...
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceID, MCI_STATUS, MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms);

iSeconds = (int)((StatusParms.dwReturn / 1000 ) % 60 ));


Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 62
Path to  STL Map header file 1 58
How can i compile this github project?? 2 84
What is sub-make ? 2 60
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question