WinCe PlaySound每次都不起作用

时间:2017-12-21 12:13:44

标签: c++ windows-ce

我的设备WinCE 5.0存在问题。我需要从资源中播放声音。

BOOL PlayResource (int lpName) 
{ 
  BOOL bRtn; 
  LPTSTR lpRes; 
  HANDLE hResInfo, hRes; 
  HINSTANCE     hInst = AfxGetInstanceHandle();

  // Find the wave resource.
  hResInfo = FindResource (hInst, MAKEINTRESOURCE(lpName), L"WAVE"); 

  if (hResInfo == NULL) 
  {
      filePlay = false;
    return FALSE; 
  }
  // Load the wave resource. 
  hRes = LoadResource (hInst, HRSRC(hResInfo)); 

  if (hRes == NULL) 
  {
      filePlay = false;
    return FALSE; 
  }
  // Lock the wave resource and play it. 
  lpRes = (LPTSTR)LockResource (hRes);

  if (lpRes != NULL) 
    { 
    bRtn = PlaySound(lpRes,GetModuleHandle(NULL), SND_MEMORY | SND_SYNC  | SND_NODEFAULT); 
    DWORD err = GetLastError();
    Logger::getInstance().log(Logger::LOG_INFO, "PlaySound return: %d error: %d", bRtn, err);
    } 
  else 
    bRtn = 0; 

  filePlay = false;
  return bRtn; 
}

PlaySound每次都不起作用。 bRtn每次都返回1,但每次都没有播放声音。 GetLastError()一直得到代码120

1 个答案:

答案 0 :(得分:0)

更改

bRtn = PlaySound(lpRes,GetModuleHandle(NULL), SND_MEMORY | SND_SYNC  | SND_NODEFAULT);

要么

bRtn = PlaySound(lpRes, NULL, SND_MEMORY | SND_SYNC  | SND_NODEFAULT);

bRtn = PlaySound(MAKEINTRESOURCE(lpName), hInst, SND_RESOURCE | SND_SYNC  | SND_NODEFAULT);

在最后一种情况下,您甚至不需要在调用PlaySound之前将声音加载到内存中。