與上一篇的「C++ 播放音頻流(PCM裸流)」 點擊打開連接 相對應,本篇是關於用C++實現錄音功能的。一樣是直接建一個win32控制檯程序而後將代碼拷過去改個文件名就能夠用,也能夠下載本人上傳的相關工程,這個工程是用VS2013寫的,若是是較低的版本就呵呵了。點擊打開連接數組
代碼部分以下:緩存
- #include "stdafx.h"
- #include <stdio.h>
- #include <Windows.h>
- #pragma comment(lib, "winmm.lib")
-
- HWAVEIN hWaveIn;
- WAVEFORMATEX waveform;
- BYTE *pBuffer1;
- WAVEHDR wHdr1;
- FILE *pf;
- int _tmain(int argc, _TCHAR* argv[])
- {
- HANDLE wait;
- waveform.wFormatTag = WAVE_FORMAT_PCM;
- waveform.nSamplesPerSec = 8000;
- waveform.wBitsPerSample = 16;
- waveform.nChannels = 1;
- waveform.nAvgBytesPerSec = 16000;
- waveform.nBlockAlign = 2;
- waveform.cbSize = 0;
-
- wait = CreateEvent(NULL, 0, 0, NULL);
-
- waveInOpen(&hWaveIn, WAVE_MAPPER, &waveform,(DWORD_PTR)wait, 0L, CALLBACK_EVENT);
-
-
- DWORD bufsize = 1024*100;
- int i = 20;
- fopen_s(&pf, "錄音測試.pcm", "wb");
- while (i--)
- {
- pBuffer1 = new BYTE[bufsize];
- wHdr1.lpData = (LPSTR)pBuffer1;
- wHdr1.dwBufferLength = bufsize;
- wHdr1.dwBytesRecorded = 0;
- wHdr1.dwUser = 0;
- wHdr1.dwFlags = 0;
- wHdr1.dwLoops = 1;
- waveInPrepareHeader(hWaveIn, &wHdr1, sizeof(WAVEHDR));
- waveInAddBuffer(hWaveIn, &wHdr1, sizeof (WAVEHDR));
- waveInStart(hWaveIn);
- Sleep(1000);
- waveInReset(hWaveIn);
- fwrite(pBuffer1, 1, wHdr1.dwBytesRecorded, pf);
- delete pBuffer1;
- printf("%ds ", i);
- }
- fclose(pf);
-
- waveInClose(hWaveIn);
- return 0;
- }
from:http://blog.csdn.net/weixinhum/article/details/31155985