Raz-Soft » Blog Archive » mciSendString: Playing with sounds Buy Cheap Software
Ploua… »
« La lumina neonului din camera…

mciSendString: Playing with sounds

  I'm some how disappointed when I see a fellow programmer using 3d party controls/dll/activeX/whatever, just for doing easy stuff like playing a stupid event sound from their application. It's very easy to play a sound (wav/mp3 even videos) it has been already implemented by M$ long time ago and if you don't want advanced stuff like The BASS library has, (which is free for non-commercial use) you can use the MCI Functions from winmm.dll library (you will find this library even on a Windows 95 system -if you can find one- :) ) . Now how to use winmm in you application?

I'm sure you will find a lot of sources if you search thro' Google or CodeProject but here is some quick ways to play/stop/pause/resume/get sound length/ get current play position functions using mciSendString (see Multimedia Command Strings for a list of commands that you can use) :

1. Opening the sound file:
BOOL SoundOpen (char *filep,char *soundID);
BOOL DoShortPath(char *fullpath,char shortpath[256]);

C++:
  1. #include <windows.h>
  2.   #include <mmsystem.h>
  3.   #include <stdio.h>
  4.  /*******************************************************************
  5.     PURPOSE:  Open a sound file from disk
  6.     PARAMS  :  [in]  char *filep : the full path to your sound file  (like: "C:\\Music\\Example.mp3")
  7.           [in]  char *soundID: specifies an alternate name for the given sound, it must be used in subsequent commands.
  8.    RETURNS : true if the sound file has been loaded successfully , false otherwise
  9. ************************************************************************/
  10. BOOL SoundOpen (char *filep,char *soundID)
  11.    {
  12.       MCIERROR mcirez;
  13.       char comm[128];
  14.       char filepath[256];
  15.       DoShortPath(filep,filepath);   //it looks like mciSendString doesn't like long path names so we have to make them short
  16.       wsprintf(comm, "open %s alias %s wait",filepath, soundID );   //build the MCI command string
  17.       mcirez=mciSendString(comm,NULL, 0, NULL); //send the MCI command
  18.       if (mcirez!=MMSYSERR_NOERROR)
  19.           return FALSE;
  20.      else
  21.        {
  22.            wsprintf(comm, "set %s time format ms", soundID );
  23.            mcirez=mciSendString (comm,NULL,0,NULL);
  24.        }
  25.        return TRUE;
  26.    }   
  27. //*******************************************************
  28. BOOL DoShortPath(char *fullpath,char *shortpath)
  29.    {
  30.        char buff[256]={0};
  31.          if(GetShortPathName (fullpath,buff,256))
  32.            {
  33.              strcpy(shortpath,buff);
  34.              return TRUE;   }
  35.           else
  36.              return FALSE;   
  37.                  
  38.    }

2. Playing the sound file:
BOOL SoundPlay (char *soundID);

C++:
  1. /****************************************************************
  2.     PURPOSE:  Start playing a opend sound
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4. *****************************************************************/
  5. BOOL SoundPlay (char *soundID)
  6.    {
  7.      MCIERROR mcirez;
  8.      char comm [128];
  9.      wsprintf(comm, "play %s ",soundID);
  10.      mcirez=mciSendString(comm,NULL, 0, NULL);
  11.      if (mcirez!=MMSYSERR_NOERROR )
  12.       {
  13.         wsprintf(comm, "close %s",soundID);
  14.         mcirez=mciSendString (comm,NULL,0,NULL);
  15.         return FALSE;
  16.       }
  17.      
  18.      return TRUE;
  19.    }

3. Pausing the sound file:
BOOL SoundPause (char *soundID);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Pause a playing sound
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4. --------------------------------------------------------------------------------*/
  5. BOOL SoundPause (char *soundID)
  6.    {
  7.      char comm [128];
  8.      char buf[256];
  9.      wsprintf(comm, "pause %s",soundID);
  10.      if(mciSendString(comm,buf, 256, NULL)!=MMSYSERR_NOERROR)
  11.        return FALSE;
  12.      else
  13.        return TRUE;
  14.    }

4. Resume the sound file:
BOOL SoundPause (char *soundID);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Resume a previus paused sound
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4. --------------------------------------------------------------------------------*/   
  5. BOOL SoundResume (char *soundID)
  6.    {
  7.      char comm [128];
  8.      char buf[256];
  9.      wsprintf(comm, "resume %s",soundID);
  10.      if(mciSendString(comm,buf, 256, NULL)!=MMSYSERR_NOERROR)
  11.        return FALSE;
  12.      else
  13.        return TRUE;
  14.     }

5. Get the sound length (in miliseconds):
long SoundLength (char *soundID);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Resume a previus paused sound
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4.     RETURNS : the sound length in miliseconds or 0 if the soundID is not valid  
  5. --------------------------------------------------------------------------------*/     
  6. long SoundLength (char *soundID)
  7.    {
  8.      MCIERROR mcirez;
  9.      char buff [256];
  10.      char comm [128];
  11.      wsprintf(comm, "status %s length",soundID);
  12.      mcirez=mciSendString(comm,buff, 256, NULL);
  13.      if (mcirez!=MMSYSERR_NOERROR)
  14.        return 0;
  15.      else
  16.       return atol(buff);
  17.      
  18.    }

6. Get the sound current play position (in miliseconds):
long SoundPosition (char *soundID);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Get the sound current play position
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4.     RETURNS : the sound current position in miliseconds or 0 if the soundID is not valid    
  5. --------------------------------------------------------------------------------*/        
  6. long SoundPosition (char *soundID)
  7.    {
  8.      MCIERROR mcirez;
  9.      char comm [128];
  10.      char buff[256];
  11.      wsprintf(comm, "status %s position",soundID);
  12.      mcirez=mciSendString(comm,buff, 256, NULL);
  13.      if (mcirez!=MMSYSERR_NOERROR)
  14.         return 0;
  15.      else
  16.        return atol(buff);   
  17.     }

6. Set the sound play position (in miliseconds):
BOOL SoundSeek (char *soundID,long *position);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Set the sound play position
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen
  4.                   [in] position : sound position in miliseconds
  5.     NOTES    : SoundSeek  moves to the specified position and stops    
  6. --------------------------------------------------------------------------------*/     
  7. BOOL SoundSeek (char *soundID,long *position)
  8.    { 
  9.      char comm [128];
  10.      wsprintf(comm, "seek %s to %d",soundID,position);
  11.      if(mciSendString(comm,NULL, 0, NULL) !=MMSYSERR_NOERROR)
  12.        return FALSE;
  13.      else
  14.       return TRUE;
  15.    }

6. Close the opened sound:
BOOL SoundClose(char *soundID);

C++:
  1. /*------------------------------------------------------------------------------
  2.     PURPOSE:  Close a opened sound
  3.     PARAMS  :  [in]  soundID : the opened sound id passed on SoundOpen  
  4. --------------------------------------------------------------------------------*/     
  5. BOOL SoundClose(char *soundID)
  6.    {
  7.       MCIERROR mcirez; 
  8.       char comm [128];
  9.       wsprintf(comm, "close %s",soundID);
  10.       mcirez=mciSendString (comm ,NULL,0,NULL);
  11.       if (mcirez!=MMSYSERR_NOERROR)
  12.        return FALSE;
  13.       else
  14.        return TRUE;
  15.    }

Here is an example on how to use the above functions to play a sound:

C++:
  1. //----------------------------------------------------------------------
  2. //opening and playing:
  3. SoundOpen("C:\\Music\\MySoundFile.mp3","mysoundid");
  4. SoundPlay("mysoundid");
  5. //close
  6. SoundClose("mysoundid");
  7. //etc....
  8.  
  9. //----------------------------------------------------------------------

Happy programming :)

related things:

Author: Raz | On December 9th, 2006 | C/C++, [ En ] | 1 Comment

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    One Response to “mciSendString: Playing with sounds”

  1. Vivian H UNITED STATES Says:

    Heya...

    Love that google, great site. Take care....


Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions