WEB开发网
开发学院软件开发VC VC下实现DirectSound 阅读

VC下实现DirectSound

 2010-05-25 20:36:48 来源:WEB开发网   
核心提示:3. 创建声音缓冲区DSBUFFERDESC BufferDesc;memset (&BufferDesc, 0, sizeof (BufferDesc));BufferDesc.lpwfxFormat = (LPWAVEFORMATEX) m_pFormat;BufferDesc.dwSize = sizeof (D

3. 创建声音缓冲区

DSBUFFERDESC BufferDesc;
memset (&BufferDesc, 0, sizeof (BufferDesc));
BufferDesc.lpwfxFormat = (LPWAVEFORMATEX) m_pFormat;
BufferDesc.dwSize = sizeof (DSBUFFERDESC);
BufferDesc.dwBufferBytes = m_dwSize;
BufferDesc.dwFlags = 0;
file://头文件中声明m_pDSoundBuffer,类
型:LPDIRECTSOUNDBUFFER
if (pDSoundObject->CreateSoundBuffer
(&BufferDesc, &m_pDSoundBuffer, 0) != DS_OK)
return FALSE;

4. 载入声音数据

BOOL CDirectWave::LoadData (void){
LPVOID lpPtr1, lpPtr2;
DWORD dwLen1, dwLen2;
HRESULT hResult;
TryLoad:
hResult = m_pDSoundBuffer- >Lock
(0, m_dwSize, &lpPtr1, &dwLen1, &lpPtr2, &dwLen2, 0);
if (hResult == DS_OK){
memcpy (lpPtr1, m_pData, dwLen1);
if (lpPtr2)
memcpy (lpPtr2, m_pData + dwLen1, dwLen2);
m_pDSoundBuffer- >Unlock
(lpPtr1, dwLen1, lpPtr2, dwLen2);
return TRUE;
}
else if (hResult == DSERR_BUFFERLOST){
hResult = m_pDSoundBuffer- >Restore ();
if (hResult == DS_OK)
goto TryLoad;
}
return FALSE;
}

5. 播放及停止

void CDirectWave::Play (BOOL bLoop){
DWORD dwFlags = 0;
file://dwFlags= DSBPLAY_LOOPING可实现循环播放
TryPlay:
if (m_pDSoundBuffer- >
Play (0, 0, dwFlags) == DSERR_BUFFERLOST){
if (LoadData ())//数据丢失,重新装载
goto TryPlay;
}
}
void CDirectWave::Stop (void){
m_pDSoundBuffer- >Stop ();
}

6. 关闭。释放内存块,声音缓冲区。

DirectSound需要用到mmsystem.h,dsound.h两个头文件。

上一页  1 2 

Tags:VC 实现 DirectSound

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接