第一部分
每当我们打开”酷狗音乐”的时候,总是可以先听到”Hello,Kugou”这样一个声音,其实这个声音文件就在KUGOU的文件夹里面,:
我现在的想法是把这个文件当作资源压入到EXE文件里,运行的时候
FindResource、LoadResource、LockResource、sndPlaySound、UnlockResource、FreeResource
到网上查阅MSDN,有一篇《Playing WAVE Resources》,给出了示例代码,地址是:
http://msdn./en-us/library/windows/desktop/dd743679(v=).aspx
,如下图所示:
我改写的如下,只是增加了一个HINSTACE型的参数hInst:
BOOL PlayResource(LPSTR lpName,HINSTANCE hInst)
{
BOOL bRtn;
LPSTR lpRes;
HANDLE hResInfo, hRes;
// Find the WAVE resource.
hResInfo = FindResource(hInst, lpName, "WAVE");
if (hResInfo == NULL)
return FALSE;
// Load the WAVE resource.
// 红色字体是我加的,因为编译器要求explicit cast
hRes = LoadResource(hInst, (HRSRC)hResInfo);
if (hRes == NULL)
return FALSE;
// Lock the WAVE resource and play it.
lpRes = (LPSTR)LockResource(hRes);
if (lpRes != NULL) {
bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC |
SND_NODEFAULT);
UnlockResource(hRes);
}
else
bRtn = 0;
// Free the WAVE resource and return ess or failure.
FreeResource(hRes);
return bRtn;
}
SND_SYNC是同步地(synchronously)播放,MSDN:
The sound is played synchronously and the function does not return until the sound ends.
声音同步播放,函数直到声音结束才返回.
如果想要循环播放的话,要加入SND_LOOP、SND_ASYNC这两个参数。
SND_ASYNC:
The sound is played asynchronously and the function returns immediately after
PlayingWAVEResources的一次尝试 来自淘豆网www.taodocs.com转载请标明出处.