PlaySound直接播放本地音频文件,速度有点滞后,可以采用先将音频文件读取到内存中,然后在播放音频的时候直接从内存中读取音频数据进行播放,播放速度会有提示。
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <windows.h>
#include "Mmsystem.h"
#include <Digitalv.h>
#pragma comment(lib, "Winmm.lib")
using namespace std;
int main()
{
char* m_AlertMusicBuffer = nullptr;
FILE* alertMusicFile;
alertMusicFile = fopen(".//alert.wav", "rb");
if (alertMusicFile != nullptr)
{
fseek(alertMusicFile, 0, SEEK_END);
long fileSize = ftell(alertMusicFile);
rewind(alertMusicFile);
int num = fileSize / sizeof(char);
m_AlertMusicBuffer = (char*)malloc(sizeof(char)*num);
if (m_AlertMusicBuffer == NULL)
{
return 0;
}
fread(m_AlertMusicBuffer, sizeof(char), num, alertMusicFile);
}
PlaySound((LPCSTR)m_AlertMusicBuffer, NULL, SND_MEMORY | SND_LOOP | SND_ASYNC);
free(m_AlertMusicBuffer);
_CrtDumpMemoryLeaks();
getchar();
return 0;
}
如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com/,更多博文干货等着您。