我的设备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

最佳答案

更改

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之前将声音加载到内存中。

07-24 14:14