Click here to Skip to main content
6,926,902 members and growing! (25,960 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

A Simple Voice Recording Application Using MCI

By chakkaradeepcc

Record your voice using microphone.
VC6, Windows, MFC, Dev
Posted:3 Oct 2004
Views:87,401
Bookmarked:55 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.16 Rating: 3.50 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
4 votes, 50.0%
4
1 vote, 12.5%
5

Introduction

Compared to my previous tutorial on recording voice using SAPI, this application records your voice through a microphone and saves it to a temporary file after you speak for 10 seconds.

Just wait for my next tutorial which will record your voice till you press "Stop" and not for seconds. This tutorial just gives you an idea on how to use MCI, the Media Control Interface.

Media Control Interface

The Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device.

Using Code

The recording scenario consists of the following steps:

  1. Open a waveform-audio device with a new file for recording.
  2. Once the device is opened successfully; get the device ID.
  3. Begin recording and record for the specified number of milliseconds. Wait for recording to complete before continuing.
  4. Assume the default time format for the waveform-audio device (milliseconds).
  5. Play the recording and query user to save the file.
  6. Save the recording to a file named TEMPFILE.WAV. Wait for the operation to complete before continuing.

See how simple the operation is, the code snippet for the above steps is as follows:

DWORD CMyRecordDlg::RecordWAVEFile(DWORD dwMilliSeconds)
{
    UINT wDeviceID;
    DWORD dwReturn;
    MCI_OPEN_PARMS mciOpenParms;
    MCI_RECORD_PARMS mciRecordParms;
    MCI_SAVE_PARMS mciSaveParms;
    MCI_PLAY_PARMS mciPlayParms;

    // Open a waveform-audio device with a new file for recording.

    mciOpenParms.lpstrDeviceType = "waveaudio";
    mciOpenParms.lpstrElementName = "";
    if (dwReturn = mciSendCommand(0, MCI_OPEN,
        MCI_OPEN_ELEMENT | MCI_OPEN_TYPE, 
        (DWORD)(LPVOID) &mciOpenParms))
    {
        // Failed to open device; don't close it, just return error.

        return (dwReturn);
    }

    // The device opened successfully; get the device ID.

    wDeviceID = mciOpenParms.wDeviceID;

    // Begin recording and record for the specified number of 

    // milliseconds. Wait for recording to complete before continuing. 

    // Assume the default time format for the waveform-audio device 

    // (milliseconds).

    mciRecordParms.dwTo = dwMilliSeconds;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_RECORD, 
        MCI_TO | MCI_WAIT, (DWORD)(LPVOID) &mciRecordParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    // Play the recording and query user to save the file.

    mciPlayParms.dwFrom = 0L;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY,
        MCI_FROM | MCI_WAIT, (DWORD)(LPVOID) &mciPlayParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }
    if (MessageBox("Do you want to save this recording?",
        "", MB_YESNO) == IDNO)
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (0L);
    }

    // Save the recording to a file named TEMPFILE.WAV. Wait for

    // the operation to complete before continuing.

    mciSaveParms.lpfilename = "tempfile.wav";
    if (dwReturn = mciSendCommand(wDeviceID, MCI_SAVE,
        MCI_SAVE_FILE | MCI_WAIT, (DWORD)(LPVOID) &mciSaveParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    return (0L);

}

Reference

  1. Microsoft Developer Network (MSDN)

My Contact

  1. chakkaradeepcc@yahoo.com

Note

  1. SAPI - Speech Application Programmer Interface.
  2. MCI - Multimedia Control Interface.

License

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

chakkaradeepcc


Member
Chakkaradeep, known as Chaks to his friends, hails from the Indian subcontinent.Having done his Masters in Software Engineering, he knows his way around a computer and has sound knowledge of Microsoft technologies.

Chaks is currently working as a Microsoft Developer @ New Zealand.

You can reach him via his blog - http://chakkaradeep.wordpress.com
Occupation: Software Developer
Location: New Zealand New Zealand

Other popular Audio and Video articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
QuestionRegarding License !! PinmemberNikhil_77779:36 18 Feb '09  
GeneralSaving the file in webserver Pinmemberyogeshkumarkollu2:31 12 Jan '09  
GeneralHow do i record only when data is there? PinmemberNikita Agarwal22:00 9 Apr '08  
Generalmore tracks PinmemberLetizia2:57 11 Jan '08  
GeneralProblem with Device Detection Pinmemberimadkk15:20 2 Nov '06  
Questioncan i do this in c# PinmemberKumar Muthu1:39 29 Oct '05  
AnswerRe: can i do this in c# Pinmemberchakkaradeepcc20:49 11 Oct '06  
AnswerRe: can i do this in c# PinmemberBarryDorman10:29 8 Dec '06  
GeneralRegarding Setaudio Pinsussvenugopalan23:17 25 Oct '05  
Generalhow can this be acheived in C# PinmemberSamar Aarkotti5:58 13 Oct '05  
I have tried few thing's but did not work for me, would apprciate if you can tell me how to achieve this in C#
GeneralVoice Activating Recording Pinmembersajeepra10:01 6 Jul '05  
GeneralStop recording..? Pinmembermpallavi20:00 5 Jun '05  
GeneralRe: Stop recording..? PinmemberGernot Frisch20:56 14 Jan '07  
Generalreally good & useful tutorial Pinmembernsavara11:22 16 Jan '05  
GeneralRe: really good & useful tutorial Pinmemberchakkaradeepcc5:53 18 Jan '05  
GeneralRip off MSDN PinsussHerbDX0:24 27 Oct '04  
GeneralRe: Rip off MSDN PinmemberEdgar Rocha11:10 25 Jul '05  
GeneralChange of parameters... PinmemberAntonella Vlassi0:53 18 Oct '04  
GeneralRe: Change of parameters... Pinmemberchakkaradeepcc21:14 11 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 3 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by chakkaradeepcc
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project