We help IT Professionals succeed at work.

getting length of  a  wav file

cww1
cww1 asked
on
322 Views
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;  
      mciFlags = MCI_OPEN_TYPE | MCI_OPEN_ELEMENT

      // 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

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

}

Comment
Watch Question

Author

Commented:
Edited text of question
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.