Click here to Skip to main content
15,170,078 members
Articles / Mobile Apps / Windows Mobile
Posted 17 Apr 2005


129 bookmarked

Voice Recording/Playing back using simple classes

Rate me:
Please Sign up or sign in to vote.
4.62/5 (43 votes)
17 Apr 20051 min read
This article introduces some useful classes that wrap the WAVE APIs.


Most of us are familiar with at least one multimedia recording software. Windows has one of the simplest ones: Sound Recorder. In this article I want to introduce some simple but useful classes for recording/playing back of voice that wrap the WAVE APIs.


Here is the list of all the Wave APIs that I have used:


waveInOpenOpen the Wave device for recording.
waveInPrepareHeaderPrepare the Wave header for recording.
waveInAddBufferAdd buffer
waveInStartStart recording. Recorded data will be saved in a specified buffer.
waveInUnprepareHeaderAfter finishing the record process, should un-prepare Wave header.
waveInCloseClose the recording device.


waveOutOpenOpen the Wave device for playing.
waveOutPrepareHeaderPrepare the Wave header for playing.
waveOutWritePlay the buffer.
waveOutUnprepareHeaderUn-prepare the Wave header.
waveOutCloseClose the playing device.

As you can see, working with these APIs are straightforward but if you want to skip them and simply do a copy/paste code in your project, my classes are for you.

Here is the definition of the classes:

class CVoiceBase  
    CString m_result;
    MMRESULT res;


    char* buffer;
    WAVEHDR WaveHeader;

    void SetFormat(DWORD nSamplesPerSec,  
            WORD  wBitsPerSample,WORD  nChannels);
    BOOL CopyBuffer(LPVOID lpBuffer, DWORD ntime);
    CString GetLastError();
    void GetMMResult(MMRESULT res);
    void DestroyBuffer();
    BOOL PrepareBuffer(DWORD ntime);
    virtual ~CVoiceBase();

class CVoiceRecording : public CVoiceBase  
    void RecordFinished();
    BOOL IsOpen();
    BOOL Close();
    BOOL Open();    
    BOOL Record();
    HWAVEIN hWaveIn;
    virtual ~CVoiceRecording();

class CVoicePlaying : public CVoiceBase  
    void PlayFinished();
    BOOL IsOpen();
    BOOL Close();
    BOOL Open();
    BOOL Play();

    HWAVEOUT hWaveOut;

    virtual ~CVoicePlaying();

Also, for notifying of events, such as finishing recording/playing, I have used two callback functions:

BOOL CALLBACK VoiceWaveInProc(HWAVEIN hwi,       
            UINT uMsg,         
            DWORD dwInstance,  
            DWORD dwParam1,    
            DWORD dwParam2     

BOOL CALLBACK VoiceWaveOutProc(HWAVEOUT hwi,       
            UINT uMsg,         
            DWORD dwInstance,  
            DWORD dwParam1,    
            DWORD dwParam2     


Just declare two variables of type CVoiceRecording and CVoicePlaying. Use SetFormat, PrepareBuffer, Open and Record or Play member functions to work with them:

CVoiceRecording m_Record;
CVoicePlaying m_Play;

m_Record.PrepareBuffer(10);    //prepare buffer for recording 10 seconds.
m_Play.PrepareBuffer(10);    //prepare buffer for playing of 10 seconds of data

if (m_Record.IsOpen())

//after finishing the record scenario,
//play the buffer, first copy recorded buffer to m_Play buffer
m_Play.CopyBuffer(m_Record.buffer, 10);
if (m_Play.IsOpen())



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Authors

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic of) Iran (Islamic Republic of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here

Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

Comments and Discussions

Questionrecording wave file from voice modem Pin
zendehdel19-Jul-14 23:47
Memberzendehdel19-Jul-14 23:47 
GeneralMy vote of 5 Pin
Member 1038781122-Jan-14 19:56
MemberMember 1038781122-Jan-14 19:56 
QuestionMMSystem.h Pin
Member 1030607430-Sep-13 0:58
professionalMember 1030607430-Sep-13 0:58 
AnswerRe: MMSystem.h Pin
Frans Jan17-Jun-14 0:34
professionalFrans Jan17-Jun-14 0:34 
GeneralRe: MMSystem.h Pin
Member 1096937831-Mar-21 13:30
MemberMember 1096937831-Mar-21 13:30 
Questionwriting a wave file to currecnt directory from buffer Pin
Member 87012356-Apr-12 5:26
MemberMember 87012356-Apr-12 5:26 
GeneralMy vote of 4 Pin
Member 87012356-Apr-12 5:24
MemberMember 87012356-Apr-12 5:24 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 20:50
professionalManoj Kumar Choubey26-Feb-12 20:50 
GeneralMy vote of 3 Pin
zwj72319-Feb-12 23:03
Memberzwj72319-Feb-12 23:03 
Generalسلام Pin
mahboob4666822-May-11 9:50
Membermahboob4666822-May-11 9:50 
GeneralI fatted this project,now can recording and playback at the same time ,minimal 50MA delay can play normal [modified] Pin
erben16-Jan-11 17:20
Membererben16-Jan-11 17:20 
QuestionCould you please write it using VB .net? Pin
lowisdom8-Jun-10 18:08
Memberlowisdom8-Jun-10 18:08 
GeneralRecord only internal sound not what ever we talk too (how).. Pin
sunil1501877-Apr-10 20:59
Membersunil1501877-Apr-10 20:59 
Generalexecuting the program doesnt play nethn Pin
niki parmar17-Mar-10 9:06
Memberniki parmar17-Mar-10 9:06 
GeneralRe: executing the program doesnt play nethn Pin
niki parmar18-Mar-10 1:14
Memberniki parmar18-Mar-10 1:14 
GeneralA Little doubt Pin
rendar_8-Nov-09 7:16
Memberrendar_8-Nov-09 7:16 
GeneralPlay an array Pin
asef31-Oct-09 1:45
Memberasef31-Oct-09 1:45 
GeneralTest of script Pin
supertotal3-Aug-09 11:42
Membersupertotal3-Aug-09 11:42 
QuestionReg: How many minutes can I buffer I have to save it to a file Pin
Nikhil_777712-Jun-09 6:48
MemberNikhil_777712-Jun-09 6:48 
QuestionHow do I make this program record until the user presses a key Pin
Nikhil_77772-Jun-09 9:02
MemberNikhil_77772-Jun-09 9:02 
QuestionRe: How do I make this program record until the user presses a key Pin
Nikhil_77773-Jun-09 11:05
MemberNikhil_77773-Jun-09 11:05 
Generalcall recording Pin
joshi aniruddha27-Apr-09 1:52
Memberjoshi aniruddha27-Apr-09 1:52 
GeneralRe: call recording Pin
vvhhbc23-Dec-09 19:20
Membervvhhbc23-Dec-09 19:20 
QuestionI successfully compiled the files. How do I save them to a file !! **Urgent** Pin
Nikhil_77775-Mar-09 12:28
MemberNikhil_77775-Mar-09 12:28 
QuestionHas anyone successfully compiled this program ?? Pin
Nikhil_777719-Feb-09 17:41
MemberNikhil_777719-Feb-09 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.