直接上代码,如果有需要可以直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用了(注意将声道和频率与你自己的文件对应哦)。当然我自己也用VS2008(VS2013好用太多,强烈推荐还是用VS2013,只是它的工程太大,上传起来相当费劲,所以还是用VS2008重写了。。)写了个例子上传了,如果有需要下载地址如下(自己没什么下载分了,所以弄了3分,见谅哈哈)
这份代码是打开文件截取一段数据然后播放的,可以轻松的经过加一条线程的方式改成网络传输的形式。
- #include "stdafx.h"
- #include <stdio.h>
- #include <windows.h>
- #pragma comment(lib, "winmm.lib")
-
- char buf[1024 * 1024 * 4];
-
- int _tmain(int argc, _TCHAR* argv[]) {
- FILE* thbgm;
- int cnt;
- HWAVEOUT hwo;
- WAVEHDR wh;
- WAVEFORMATEX wfx;
- HANDLE wait;
-
- wfx.wFormatTag = WAVE_FORMAT_PCM;
- wfx.nChannels = 1;
- wfx.nSamplesPerSec = 8000;
- wfx.nAvgBytesPerSec = 16000;
-
- wfx.nBlockAlign = 2;
- wfx.wBitsPerSample = 16;
- wfx.cbSize = 0;
- wait = CreateEvent(NULL, 0, 0, NULL);
- waveOutOpen(&hwo, WAVE_MAPPER, &wfx, (DWORD_PTR)wait, 0L, CALLBACK_EVENT);
-
- fopen_s(&thbgm, "paomo.pcm", "rb");
- cnt = fread(buf, sizeof(char), 1024 * 1024 * 4, thbgm);
-
- int dolenght = 0;
- int playsize = 1024;
- while (cnt) {
-
- wh.lpData = buf + dolenght;
- wh.dwBufferLength = playsize;
- wh.dwFlags = 0L;
- wh.dwLoops = 1L;
- waveOutPrepareHeader(hwo, &wh, sizeof(WAVEHDR));
- waveOutWrite(hwo, &wh, sizeof(WAVEHDR));
- WaitForSingleObject(wait, INFINITE);
-
- dolenght = dolenght + playsize;
- cnt = cnt - playsize;
- }
- waveOutClose(hwo);
- fclose(thbgm);
- return 0;
- }
(秩名) |