如何给程序增加音乐啊??
我做了个俄罗斯方块,个人想给它加点音乐希望各位大哥大姐大侠们指点一下下。。。。。谢谢了。。。。
playsound
bool LoadFile(LPCTSTR sFile, LPVOID & outData){
struct _stat buf;//struct _stat 获取普通文件
if(_stat(sFile,&buf))
return false;
outData = new char[buf.st_size];
FILE *fp = NULL;
fopen_s(&fp,sFile,"rb");//以二进制形式打开文件
fread(outData,buf.st_size,1,fp);
fclose(fp);
return true;
}
BOOL GetWaveData(void * pRes, WAVEFORMATEX * & pWaveHeader, void * & pbWaveData, DWORD & cbWaveSize){
pWaveHeader = 0;
pbWaveData = 0;
cbWaveSize = 0;
DWORD *pdw = (DWORD*)pRes;
DWORD dwLength = *pdw++;
DWORD dwRiff = *pdw++;
DWORD dwType = *pdw++;
if(dwRiff!=mmioFOURCC('R','I','F','F'))
return false;
if(dwType!=mmioFOURCC('W','A','V','E'))
return false;
DWORD*pdwEnd = (DWORD*)((BYTE*)pdw+dwLength-4);
while(pdw<pdwEnd){
dwType = *pdw++;
dwLength = *pdw++;
switch( dwType )
{
case mmioFOURCC('f', 'm', 't', ' '):
if( !pWaveHeader )
{
if( dwLength < sizeof(WAVEFORMAT) )
return false; // not a WAV
pWaveHeader = (WAVEFORMATEX *)pdw;
if( pbWaveData && cbWaveSize )
return true;
}
break;
case mmioFOURCC('d', 'a', 't', 'a'):
pbWaveData = LPVOID(pdw);
cbWaveSize = dwLength;
if( pWaveHeader )
return true;
break;
}
pdw = (DWORD *)((BYTE *)pdw + ((dwLength+1)&~1));
}
return false;
}
BOOL MyCreateSoundBuffer(WAVEFORMATEX * pcmwf){
DSBUFFERDESC dsbdesc;
// Set up DSBUFFERDESC structure.
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); // Zero it out.
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
// Need no controls (pan, volume, frequency).
dsbdesc.dwFlags = DSBCAPS_STATIC; // assumes that the sound is played often
dsbdesc.dwBufferBytes = m_dwTheSound;
dsbdesc.lpwfxFormat = pcmwf; // Create buffer.
HRESULT hRes;
hRes = m_ds->CreateSoundBuffer(&dsbdesc, &m_pDsb, 0);
if (FAILED(hRes))
{
TRACE("MyCreateSoundBuffer FAILED!");
return FALSE;
}
return TRUE;
}
BOOL SetSoundData(void * pSoundData, DWORD dwSoundSize){
LPVOID lpvPtr1;
DWORD dwBytes1;
// Obtain write pointer.
HRESULT hr = m_pDsb->Lock(0, 0, &lpvPtr1, &dwBytes1, 0, 0, DSBLOCK_ENTIREBUFFER);
// If DSERR_BUFFERLOST is returned, restore and retry lock.
if(DSERR_BUFFERLOST == hr)
{
m_pDsb->Restore();
hr = m_pDsb->Lock(0, 0, &lpvPtr1, &dwBytes1, 0, 0, DSBLOCK_ENTIREBUFFER);
}
if(DS_OK == hr)
{
// Write to pointers.
CopyMemory(lpvPtr1, pSoundData, dwBytes1);
// Release the data back to DirectSound.
hr = m_pDsb->Unlock(lpvPtr1, dwBytes1, 0, 0);
if(DS_OK == hr)
return TRUE;
}
// Lock, Unlock, or Restore failed.
return FALSE;
}
void Play(){
//TRACE("play");
m_pDsb->SetCurrentPosition(0);
if( DSERR_BUFFERLOST == m_pDsb->Play(0, 0, 0) )
{
// another application had stolen our buffer
// Note that a "Restore()" is not enough, because
// the sound data is invalid after Restore().
SetSoundData(m_pTheSound, m_dwTheSound);
// Try playing again
m_pDsb->Play(0, 0, 0);
}
}
void Stop(){
m_pDsb->Stop();
}