已解决,将PlaySound()函数参数列表改为如下即可解决,不需单独添加if条件:
PlaySound(str, NULL, SND_NOSTOP | SND_FILENAME | SND_ASYNC | SND_LOOP);
另如需播放多个不同音频,可使用线程播放:
程序代码:
CEvent PlayDLBegin;
CEvent PlayDLEnd;
CEvent PlayDLPause;
CEvent PlayDLResume;
extern UINT PlayDLSound(LPVOID pParam);
UINT PlayDLSound(LPVOID pParam)
{
LPCTSTR lpres = (LPCTSTR)pParam;
static BOOL isReEntry = TRUE;
if (!isReEntry)
return 0;
isReEntry = FALSE;
::WaitForSingleObject(PlayDLBegin.m_hObject, INFINITE);
HINSTANCE h = AfxGetInstanceHandle();
HRSRC hr = FindResource(h, lpres, "WAVE");
HGLOBAL hg = LoadResource(h, hr);
LPSTR lp = (LPSTR)LockResource(hg);
BOOL KeepRunning = TRUE;
BOOL Playing = TRUE;
isReEntry = FALSE;
while (KeepRunning)
{
int Pause = ::WaitForSingleObject(PlayDLPause.m_hObject, 0);
if (Pause == WAIT_OBJECT_0)
Playing = FALSE;
int start = ::WaitForSingleObject(PlayDLResume.m_hObject, 0);
if (start == WAIT_OBJECT_0)
Playing = TRUE;
for (int j = 0; j<2; j++)
{
if (Playing)
sndPlaySound(lp, SND_MEMORY | SND_SYNC);
Sleep(1000);
}
KeepRunning = false;
}
isReEntry = TRUE;
FreeResource(hg);
return 0;
}
AfxBeginThread(PlayDLSound, (LPVOID)str, THREAD_PRIORITY_NORMAL);
PlayDLBegin.SetEvent();
PlayDLEnd.SetEvent();
PlayDLPause.SetEvent();
PlayDLResume.SetEvent();
[此贴子已经被作者于2020-11-18 15:25编辑过]