Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:42:24+00:00 2026-05-30T15:42:24+00:00

I want to play the audio that was received from IP Camera. The format

  • 0

I want to play the audio that was received from IP Camera. The format of compressed audio data is G726 ADPCM.

I have searched for a few days on internet, but I haven’t done it yet.

I have tried many many ways to play back it. Tried to decode to PCM and build header to create Wave file, but all was failed (Maybe i had some mistake while do it).

But today, I found the code for recording and playing wave audio from PC.

Here is code:

// ADPCM.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <MMSystem.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    const int NUMPTS = 8000*10;//44100 * 10;
    int sampleRate = 8000;//44100;
    short int waveIn[NUMPTS];

    HWAVEIN hWaveIn;
    WAVEHDR WaveInHdr;
    MMRESULT result;
    HWAVEOUT hWaveOut;

    WAVEFORMATEX pFormat;
    pFormat.wFormatTag = WAVE_FORMAT_PCM;
    pFormat.nChannels = 1;
    pFormat.nSamplesPerSec = sampleRate;
    pFormat.nAvgBytesPerSec = 2 * sampleRate;
    pFormat.nBlockAlign = 2;
    pFormat.wBitsPerSample = 16;
    pFormat.cbSize = 0;

    result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

    if(result)
    {
        char fault[256];
        waveInGetErrorTextA(result, fault, 256);
        MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    WaveInHdr.lpData = (LPSTR)waveIn;
    WaveInHdr.dwBufferLength = 2 * NUMPTS;
    WaveInHdr.dwBytesRecorded = 0;
    WaveInHdr.dwUser = 0;
    WaveInHdr.dwFlags = 0;
    WaveInHdr.dwLoops = 0;
    waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

    result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
    if(result)
    {
        MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    result = waveInStart(hWaveIn);
    if(result)
    {
        MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
        return 1;
    }

    cout << "Recording..." << endl;
    Sleep((NUMPTS/sampleRate) * 1000); //Sleep while recording

    cout << "Playing..." << endl;

    if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT))
    {
        MessageBoxA(NULL, "Failed to replay", NULL, MB_OK | MB_ICONEXCLAMATION );
    }

    waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr));
    Sleep((NUMPTS/sampleRate) * 1000); //Sleep for as long as there was recorded

    waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR));
    waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
    waveInClose(hWaveIn);
    waveOutClose(hWaveOut);

    return 0;
}

I had the PCM data stored in my buffer and I want to modify the above code to play it, so i have edited it as the following code:

const int NUMPTS = 4000;//44100 * 10;
    int sampleRate = 8000;//44100;
    CHAR waveIn[NUMPTS];

    HWAVEIN hWaveIn;
    WAVEHDR WaveInHdr;
    MMRESULT result;
    HWAVEOUT hWaveOut;

    WAVEFORMATEX pFormat;
    pFormat.wFormatTag = WAVE_FORMAT_PCM;
    pFormat.nChannels = 1;
    pFormat.nSamplesPerSec = sampleRate;
    pFormat.nAvgBytesPerSec = sampleRate/2;//2 * sampleRate;
    pFormat.nBlockAlign = 1;//2;
    pFormat.wBitsPerSample = 4;//16;
    pFormat.cbSize = 0;


    WaveInHdr.lpData = (LPSTR)waveIn;
    WaveInHdr.dwBufferLength = 2 * NUMPTS;
    WaveInHdr.dwBytesRecorded = 0;
    WaveInHdr.dwUser = 0;
    WaveInHdr.dwFlags = 0;
    WaveInHdr.dwLoops = 0;
    waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

    memcpy(WaveInHdr.lpData, myPCMBuffer, NUMPTS);

    waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr));

    waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR));
    waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
    waveInClose(hWaveIn);
    waveOutClose(hWaveOut);

But It threw the message “Run-Time Check Failure #3 – The variable ‘hWaveIn’ is being used without being initialized” when waveOutUnprepareHeader() is called.

How to init hWaveIn?

This is the first time I work with wave audio therefore I pretty stupid ^^.

Can you show me the correct way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:42:25+00:00Added an answer on May 30, 2026 at 3:42 pm

    To initilize hWaveIn open the audio device:

    MMRESULT waveInOpen(
      LPHWAVEIN phwi, 
      UINT uDeviceID, 
      LPCWAVEFORMATEX pwfx, 
      DWORD dwCallback, 
      DWORD dwInstance, 
      DWORD fdwOpen 
    );
    

    Example:

    result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
                0L, 0L, WAVE_FORMAT_DIRECT);
     if (result)
     {
      char fault[256];
      waveInGetErrorText(result, fault, 256);
      Application->MessageBox(fault, "Failed to open waveform input device.",
                  MB_OK | MB_ICONEXCLAMATION);
      return;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MP3 audio file that I want to play from my Android
I have flv files that contain audio tags with a aac raw data. Each
I want to play some videos with audio from different audio files. For this
I want to play back WAV sound (audio track of some custom video format)
I have multiple files that I play in my android code using mediaplayer.I want
I have an audio file that I want to begin playing at a 20
I want to play Audio in flex application.I am using SWFLoader for it.Is there
Apparently Java only plays .au audio files and I want to play mp3 files.
Hey i want to develop a player on Android, which can play streaming audio
I create an audio buffer and want to edit this before I play this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.