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]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <windows.h> #include <mmsystem.h> #include <stdio.h> /******************************************************************* PURPOSE: Open a sound file from disk PARAMS : [in] char *filep : the full path to your sound file (like: "C:\\Music\\Example.mp3") [in] char *soundID: specifies an alternate name for the given sound, it must be used in subsequent commands. RETURNS : true if the sound file has been loaded successfully , false otherwise ************************************************************************/ BOOL SoundOpen (char *filep,char *soundID) { MCIERROR mcirez; char comm[128]; char filepath[256]; DoShortPath(filep,filepath); //it looks like mciSendString doesn't like long path names so we have to make them short wsprintf(comm, "open %s alias %s wait",filepath, soundID ); //build the MCI command string mcirez=mciSendString(comm,NULL, 0, NULL); //send the MCI command if (mcirez!=MMSYSERR_NOERROR) return FALSE; else { wsprintf(comm, "set %s time format ms", soundID ); mcirez=mciSendString (comm,NULL,0,NULL); } return TRUE; } //******************************************************* BOOL DoShortPath(char *fullpath,char *shortpath) { char buff[256]={0}; if(GetShortPathName (fullpath,buff,256)) { strcpy(shortpath,buff); return TRUE; } else return FALSE; } |
2. Playing the sound file:
BOOL SoundPlay (char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /**************************************************************** PURPOSE: Start playing a opend sound PARAMS : [in] soundID : the opened sound id passed on SoundOpen *****************************************************************/ BOOL SoundPlay (char *soundID) { MCIERROR mcirez; char comm [128]; wsprintf(comm, "play %s ",soundID); mcirez=mciSendString(comm,NULL, 0, NULL); if (mcirez!=MMSYSERR_NOERROR ) { wsprintf(comm, "close %s",soundID); mcirez=mciSendString (comm,NULL,0,NULL); return FALSE; } return TRUE; } |
3. Pausing the sound file:
BOOL SoundPause (char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /*------------------------------------------------------------------------------ PURPOSE: Pause a playing sound PARAMS : [in] soundID : the opened sound id passed on SoundOpen --------------------------------------------------------------------------------*/ BOOL SoundPause (char *soundID) { char comm [128]; char buf[256]; wsprintf(comm, "pause %s",soundID); if(mciSendString(comm,buf, 256, NULL)!=MMSYSERR_NOERROR) return FALSE; else return TRUE; } |
4. Resume the sound file:
BOOL SoundPause (char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /*------------------------------------------------------------------------------ PURPOSE: Resume a previus paused sound PARAMS : [in] soundID : the opened sound id passed on SoundOpen --------------------------------------------------------------------------------*/ BOOL SoundResume (char *soundID) { char comm [128]; char buf[256]; wsprintf(comm, "resume %s",soundID); if(mciSendString(comm,buf, 256, NULL)!=MMSYSERR_NOERROR) return FALSE; else return TRUE; } |
5. Get the sound length (in miliseconds):
long SoundLength (char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /*------------------------------------------------------------------------------ PURPOSE: Resume a previus paused sound PARAMS : [in] soundID : the opened sound id passed on SoundOpen RETURNS : the sound length in miliseconds or 0 if the soundID is not valid --------------------------------------------------------------------------------*/ long SoundLength (char *soundID) { MCIERROR mcirez; char buff [256]; char comm [128]; wsprintf(comm, "status %s length",soundID); mcirez=mciSendString(comm,buff, 256, NULL); if (mcirez!=MMSYSERR_NOERROR) return 0; else return atol(buff); } |
6. Get the sound current play position (in miliseconds):
long SoundPosition (char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /*------------------------------------------------------------------------------ PURPOSE: Get the sound current play position PARAMS : [in] soundID : the opened sound id passed on SoundOpen RETURNS : the sound current position in miliseconds or 0 if the soundID is not valid --------------------------------------------------------------------------------*/ long SoundPosition (char *soundID) { MCIERROR mcirez; char comm [128]; char buff[256]; wsprintf(comm, "status %s position",soundID); mcirez=mciSendString(comm,buff, 256, NULL); if (mcirez!=MMSYSERR_NOERROR) return 0; else return atol(buff); } |
6. Set the sound play position (in miliseconds):
BOOL SoundSeek (char *soundID,long *position);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /*------------------------------------------------------------------------------ PURPOSE: Set the sound play position PARAMS : [in] soundID : the opened sound id passed on SoundOpen [in] position : sound position in miliseconds NOTES : SoundSeek moves to the specified position and stops --------------------------------------------------------------------------------*/ BOOL SoundSeek (char *soundID,long *position) { char comm [128]; wsprintf(comm, "seek %s to %d",soundID,position); if(mciSendString(comm,NULL, 0, NULL) !=MMSYSERR_NOERROR) return FALSE; else return TRUE; } |
6. Close the opened sound:
BOOL SoundClose(char *soundID);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /*------------------------------------------------------------------------------ PURPOSE: Close a opened sound PARAMS : [in] soundID : the opened sound id passed on SoundOpen --------------------------------------------------------------------------------*/ BOOL SoundClose(char *soundID) { MCIERROR mcirez; char comm [128]; wsprintf(comm, "close %s",soundID); mcirez=mciSendString (comm ,NULL,0,NULL); if (mcirez!=MMSYSERR_NOERROR) return FALSE; else return TRUE; } |
Here is an example on how to use the above functions to play a sound:
1 2 3 4 5 6 7 8 9 | //---------------------------------------------------------------------- //opening and playing: SoundOpen("C:\\Music\\MySoundFile.mp3","mysoundid"); SoundPlay("mysoundid"); //close SoundClose("mysoundid"); //etc.... //---------------------------------------------------------------------- |
Happy programming ![]()



One Response to 'mciSendString: Playing with sounds'