想做一个录音程序
没想到刚开始就卡住了。估计是内存分配错误。有高手帮忙解答一下啊
源代码:// mic2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <iostream.h>
#include <windows.h>
#include <stdlib.h>
//加载winmm.lib库和 mmsystem.h头文件
#include <mmsystem.h>
#pragma comment(lib,"winmm")
#define INP_BUFFER_SIZE 16384 //缓存块大小
PBYTE pBuffer1,pBuffer2,pSaveBuffer,pNewBuffer;//缓存块声明
PWAVEHDR pWaveHdr1,pWaveHdr2;//准备块头
WAVEFORMATEX waveform;//音频格式
HWAVEIN hWaveIn;//Handle to the waveform-audio input device.
int main(int argc, char* argv[])
{
///////////////////////////////////////////////
////////////开始录音//////////////////////////
//////////////////////////////////////////////
///////////////准备好缓存块(两个缓存块相互替换)///////////////////
//allocate buffer memory
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
if (!pBuffer1 || !pBuffer2) {
if (pBuffer1) free(pBuffer1);
if (pBuffer2) free(pBuffer2);
cout<<"分配内存成功"<<endl;
}
//open waveform audo for input
waveform.wFormatTag=WAVE_FORMAT_PCM;
waveform.nChannels=1;
waveform.nSamplesPerSec=11025;
waveform.nAvgBytesPerSec=11025;
waveform.nBlockAlign=1;
waveform.wBitsPerSample=8;
waveform.cbSize=0;
if (waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,0,NULL,CALLBACK_WINDOW))
{
free(pBuffer1);
free(pBuffer2);
cout<<"打开设备失败"<<endl;
}
pWaveHdr1->lpData=(LPTSTR)pBuffer1;
pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr1->dwBytesRecorded=0;
pWaveHdr1->dwUser=0;
pWaveHdr1->dwFlags=0;
pWaveHdr1->dwLoops=1;
pWaveHdr1->lpNext=NULL;
pWaveHdr1->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
pWaveHdr2->lpData=(LPTSTR)pBuffer2;
pWaveHdr2->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr2->dwBytesRecorded=0;
pWaveHdr2->dwUser=0;
pWaveHdr2->dwFlags=0;
pWaveHdr2->dwLoops=1;
pWaveHdr2->lpNext=NULL;
pWaveHdr2->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));
//////////////////////////////////////////////////////////////////////////
pSaveBuffer = (PBYTE)realloc (pSaveBuffer, 1) ;//再分配内存
// Add the buffers
waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
// Begin sampling
waveInStart (hWaveIn) ;
return 0;
编译通过,不过运行的时候窗口就关闭了。。求解~~~