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);
getchar();
return 0;
}