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]);
-
#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);
-
/****************************************************************
-
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);
-
/*------------------------------------------------------------------------------
-
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);
-
/*------------------------------------------------------------------------------
-
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);
-
/*------------------------------------------------------------------------------
-
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);
-
/*------------------------------------------------------------------------------
-
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);
-
/*------------------------------------------------------------------------------
-
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);
-
/*------------------------------------------------------------------------------
-
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:
-
//----------------------------------------------------------------------
-
//opening and playing:
-
SoundOpen("C:\\Music\\MySoundFile.mp3","mysoundid");
-
SoundPlay("mysoundid");
-
//close
-
SoundClose("mysoundid");
-
//etc....
-
-
//----------------------------------------------------------------------
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.
Spammers: Beware of » my Dog! «





One Response to “mciSendString: Playing with sounds”
November 20th, 2008 at 8:16 pm
Heya...
Love that google, great site. Take care....