Click here to Skip to main content
11,412,833 members (73,964 online)
Click here to Skip to main content

How to play and record sounds

, 12 Mar 2001
Rate this:
Please Sign up or sign in to vote.
A simple application that shows how to play and record sounds under Windows


This is a simple application that shows you how to play and record sound under windows.
It uses the old multimedia API. A better solution may be to use DirectSound.

Quick Guide to the Code

Start with the two functions in CFisterDlg called OnPlay and OnRecord. Follow them down to the depth you need to use the classes.

Short description

CSoundIn is a wrapper class that will let you retreive sound from the soundcard. The main functions are Start() and Stop()

CSoundOut is a wrapper class that will let you play sound on the soundcard. The main functions are Start() and Stop()

CSoundFile is a wrapper class of a single wave file, it can either be a file reader or a file writer object. See the constructor.

CSoundBase is a very small class that encapsulates the wave format.

CBuffer is a very small class that encapsulates a simple one dimentional buffer.

The project has a number of different callback functions:

  • One callback function makes the Play button change it's label to stop when it has finished playing the file. CDialogDlg enherits CPipe and overloads a function that CPipe can call when it has finished playing the wave file.
  • Another callback function make it possible for CSoundIn to callback to CPipe when it has filled the input buffer. Thus CPipe can give CSoundIn a new buffer to fill.
  • A clone of the above principle is also used in CSoundOut, which enables it to callback to the owner when it is finished playing the sound in a given buffer.

Problems that I encountered

I have spent almost 2 days debugging the following stupid problem. When CSoundIn and CSoundOut inherit from CSoundBase and CWinThread the order in which they are listed must be as shown below. If not, the callback functions, which are started by the WinThread message handler, will not be able to access the member variables of the CSoundIn object.

class CSoundIn : public CWinThread, public CSoundBase

During these two days of fustration I also tried to implement the callback as regular callback functions called by the device driver. This is possible using ::waveInOpen(...). But since this callback function is not allowed to call any of the ::waveInXXX(...) functions it is not of much use.


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 Author

Thomas Holme
Web Developer
Denmark Denmark
No Biography provided

Comments and Discussions

Questionaudioport Pin
faezeh sadat at 6-Apr-13 13:31
memberfaezeh sadat6-Apr-13 13:31 
GeneralMy vote of 5 Pin
Dr Bob at 12-Jan-12 11:24
memberDr Bob12-Jan-12 11:24 
QuestionThis sample is very portable, but has some wrong points. Pin
Yohei Murakami at 29-Dec-11 15:50
memberYohei Murakami29-Dec-11 15:50 
QuestionI can not record fix period time using winmm.lib!!!!! [modified] Pin
faird at 1-Aug-11 19:26
memberfaird1-Aug-11 19:26 
QuestionHow do I save my recorded sound using this application? Pin
gameBaaj at 19-Feb-11 3:08
membergameBaaj19-Feb-11 3:08 
Generalproblem with stalls due to waveoutreset Pin
leehartin at 27-Jan-10 6:12
memberleehartin27-Jan-10 6:12 
Questionwhat technique did u use Pin
Adnan Aman at 13-May-09 2:09
memberAdnan Aman13-May-09 2:09 
QuestionHelp On sondcard recording upon data arrival Pin
funboyhere at 16-Jun-08 5:30
memberfunboyhere16-Jun-08 5:30 
GeneralNeed Help in Looping Sound Pin
demxine at 25-Nov-07 22:14
memberdemxine25-Nov-07 22:14 
GeneralRe: Need Help in Looping Sound Pin
KarstenK at 12-Dec-07 22:48
memberKarstenK12-Dec-07 22:48 
GeneralWhy i can't even open the zip file?Help.......... Pin
D'nise at 1-Sep-07 1:09
memberD'nise1-Sep-07 1:09 
AnswerRe: Why i can't even open the zip file?Help.......... Pin
rajneshmalik at 1-Sep-07 1:55
memberrajneshmalik1-Sep-07 1:55 
Questionrecording blank sound file Pin
rakeshkumarsonu at 1-Aug-07 22:19
memberrakeshkumarsonu1-Aug-07 22:19 
GeneralErrors!! Pin
kaunreeves at 6-May-07 2:29
memberkaunreeves6-May-07 2:29 
GeneralRe: Errors!! Pin
arler at 29-Jan-08 18:30
memberarler29-Jan-08 18:30 
GeneralHelp Pin
kaunreeves at 5-May-07 17:59
memberkaunreeves5-May-07 17:59 
Generalunable to record the data from microphone Pin
pinto22 at 12-Feb-07 4:43
memberpinto2212-Feb-07 4:43 
AnswerRe: unable to record the data from microphone Pin
Pehuenco at 26-Feb-07 0:07
memberPehuenco26-Feb-07 0:07 
GeneralRe: unable to record the data from microphone Pin
-=Seph=- at 25-Mar-07 22:04
member-=Seph=-25-Mar-07 22:04 
GeneralRe: unable to record the data from microphone Pin
Figu.Lin at 10-Feb-09 17:30
memberFigu.Lin10-Feb-09 17:30 
GeneralHelp me with default recording device!!! Pin
Hung In at 26-Jul-06 17:01
memberHung In26-Jul-06 17:01 
GeneralHi Pin
LuckyBoydtt at 13-Jul-06 9:05
memberLuckyBoydtt13-Jul-06 9:05 
GeneralRe: Hi Pin
Andy Byron at 22-Dec-06 4:47
memberAndy Byron22-Dec-06 4:47 
GeneralRe: Hi Pin
LuckyBoydtt at 22-Dec-06 5:05
memberLuckyBoydtt22-Dec-06 5:05 
Generalt Pin
angshu12011984 at 16-Jun-06 0:54
memberangshu1201198416-Jun-06 0:54 
if we want to record sound in d fister program
without using play , record button but using indiactor soubnd
how can we proceed?
GeneralHelp me! Pin
trananh at 19-Jan-06 16:03
membertrananh19-Jan-06 16:03 
GeneralError Message: Still 3 buffers in waveIn queue Pin
srettig at 15-Nov-05 12:01
membersrettig15-Nov-05 12:01 
QuestionCan i do this in C# Pin
Kumar Muthu at 29-Oct-05 1:26
memberKumar Muthu29-Oct-05 1:26 
GeneralMore help in rectifying errors Pin
armimex2000 at 28-Oct-05 14:46
memberarmimex200028-Oct-05 14:46 
AnswerRe: More help in rectifying errors Pin
Gokcer Gokdal at 10-Nov-05 12:34
memberGokcer Gokdal10-Nov-05 12:34 
GeneralAudio and Video Pin
Kisi007 at 19-Oct-05 8:33
memberKisi00719-Oct-05 8:33 
GeneralSave the Recorded Sound to a .Wav File, Pin
REU at 7-Oct-05 9:43
memberREU7-Oct-05 9:43 
GeneralRe: Save the Recorded Sound to a .Wav File, Pin
yasar79 at 10-Jan-06 2:08
memberyasar7910-Jan-06 2:08 
Generalselecting two input sources Pin
rajeev_geom at 7-Oct-05 2:03
memberrajeev_geom7-Oct-05 2:03 
GeneralVery nice design Pin
Gokcer Gokdal at 24-Aug-05 14:13
memberGokcer Gokdal24-Aug-05 14:13 
QuestionHow can I record playback sound when Only Microphone is selected in Window Record Control Pin
jackshelingzhang at 6-Jun-05 3:31
memberjackshelingzhang6-Jun-05 3:31 
Generalhelp in rectifying the errors Pin
nagulmeera at 4-Dec-04 1:49
membernagulmeera4-Dec-04 1:49 
GeneralRe: help in rectifying the errors Pin
Jordan Walters at 29-Sep-05 3:10
memberJordan Walters29-Sep-05 3:10 
GeneralRecord and Play the Sound in two different machine. Pin
Josep Ignatius at 23-Nov-04 0:56
sussJosep Ignatius23-Nov-04 0:56 
Generalhelp me,plz Pin
chaitanya1232 at 15-Jul-04 23:14
susschaitanya123215-Jul-04 23:14 
GeneralA simple way to play sound... Pin
Mark Sparrow at 24-May-04 19:42
sussMark Sparrow24-May-04 19:42 
GeneralRe: A simple way to play sound... Pin
DavidCrow at 8-Jun-04 4:21
memberDavidCrow8-Jun-04 4:21 
GeneralRe: A simple way to play sound... ? Pin
mohsen nourian at 3-Jul-04 23:05
membermohsen nourian3-Jul-04 23:05 
GeneralRe: A simple way to play sound... ? Pin
wavefan at 4-Jul-04 0:33
susswavefan4-Jul-04 0:33 
General::PostQuitMessage(0)!!! Pin
Andrew Altham at 31-Jan-04 3:58
memberAndrew Altham31-Jan-04 3:58 
Generalplay more than one file at a moment Pin
Anonymous at 19-Jan-04 23:54
sussAnonymous19-Jan-04 23:54 
GeneralCSoundIn::Stop() change to avoid "still %d buffers in waveIn queue" Pin at 1-Jan-04 2:55
memberfilipverschueren@hotmail.com1-Jan-04 2:55 
GeneralRe: CSoundIn::Stop() change to avoid "still %d buffers in waveIn queue" Pin
ze_seb at 25-Mar-05 7:39
sussze_seb25-Mar-05 7:39 
Generalplease help me to solve the link errors in the program of how to play and record Pin
elaine tan at 2-Dec-03 6:17
memberelaine tan2-Dec-03 6:17 
QuestionHow to support a sound input device in C# Pin
CNWORM at 22-Aug-03 21:39
memberCNWORM22-Aug-03 21:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 13 Mar 2001
Article Copyright 2001 by Thomas Holme
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid