C++PlaySound函数,如何用PlaySound函数来播放一个声音资源文件?

个人用的VS2013,求用PlaySound函数来播放一个ID为“IDR_WAVE_BG”的声音资源文件,越简单越好。... 个人用的VS2013,求用PlaySound函数来播放一个ID为“IDR_WAVE_BG”的声音资源文件,越简单越好。 展开
 我来答
zhwghl
2015-05-26 · TA获得超过526个赞
知道小有建树答主
回答量:137
采纳率:0%
帮助的人:36.3万
展开全部
#include "stdafx.h"
#include <Windows.h>
#include <mmsystem.h>
#include <iostream>
#include "resource.h"
#include <libloaderapi.h>
#include <WinUser.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int soundResourceID = IDR_WAVE1;
     HINSTANCE hInst = NULL;
     try{
      BOOL bRtn;
      LPVOID lpRes;
      HANDLE  hRes;
      HRSRC hResInfo;
      // Find the WAVE resource.
      hResInfo = FindResource(hInst, MAKEINTRESOURCEW(soundResourceID), L"WAVE");
      if (hResInfo == NULL)
           throw std::exception("Cannot find the resource");
      // Load the WAVE resource.
      hRes = LoadResource(hInst, hResInfo);
      if (hRes == NULL)
          throw std::exception("Cannot load the resource");
      // Lock the WAVE resource and play it.
      lpRes = LockResource(hRes);
      if (lpRes != NULL) {
           bRtn = sndPlaySoundW((wchar_t*)lpRes, SND_MEMORY | SND_SYNC |
                SND_NODEFAULT);
          UnlockResource(hRes);
          MessageBoxW(NULL, L"Playing sound", L"", 0);
      }
      else
           bRtn = 0;
          // Free the WAVE resource and return success or failure.
          FreeResource(hRes);
     }
     catch (const std::exception& e)
     {
          MessageBoxA(NULL, e.what(), "", 0);
     }
    
     return 0;
}

需要注意 不要忘记代码中的 #include 这几项,还有需要在项目(不是解决方案)的属性页面中进行如上图的设置 在 Configuration Properties->Linker->Input->Additional Dependences 中加入 winmm.lib .

 

另外补充一下如何打开 项目属性页面,如下图所示

我还上传了源代码,请查看附件

对于您的问题 请参考 https://msdn.microsoft.com/en-us/library/dd743679(v=vs.85).asp

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式