使用DSound播放声音

HWND g_hWnd;
IDirectSound8* g_pSound;
IDirectSoundBuffer8* g_pSBuffer;
CWaveFile* g_pWaveFile;
DirectSoundCreate8(0, &g_pSound, 0);
g_pSound->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY);
g_pWaveFile = new CWaveFile;
ZeroMemory(g_pWaveFile, sizeof(CWaveFile));
HRESULT hr;
hr =  g_pWaveFile->Open(L"test1.wav", 0, WAVEFILE_READ);
DSBUFFERDESC dsbd;
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags =DSBCAPS_CTRLPAN |   DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY;
dsbd.dwBufferBytes = g_pWaveFile->GetSize();
dsbd.lpwfxFormat = g_pWaveFile->m_pwfx;
LPDIRECTSOUNDBUFFER pBuffer = 0;
g_pSound->CreateSoundBuffer(&dsbd, &pBuffer, 0);
g_pSBuffer = 0;
pBuffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&g_pSBuffer);
pBuffer->Release();
void* pvoid = 0;
DWORD nLen = 0;
DWORD dwWrite = 0;
hr = 0;
hr = g_pSBuffer->Lock(0, 0, &pvoid, &nLen, 0, 0, DSBLOCK_ENTIREBUFFER);
g_pWaveFile->Read((BYTE*)pvoid, nLen, &dwWrite);
g_pSBuffer->Unlock(pvoid, nLen, 0, 0);
g_pSBuffer->SetCurrentPosition(0);
g_pSBuffer->Play(0, 0, DSBPLAY_LOOPING);

//已测试能用,其中g_hWnd的值需要给出

posted on 2012-04-23 16:53  紫澜  阅读(1650)  评论(0编辑  收藏  举报