使用DSound播放声音

本文介绍了一种使用DirectX播放音频文件的方法。通过初始化DirectX组件、创建声音缓冲区并加载WAV文件到缓冲区中,实现了音频的循环播放。此代码适用于需要直接操作DirectX进行音频播放的应用场景。
摘要由CSDN通过智能技术生成

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的值需要给出

转载于:https://www.cnblogs.com/ketmales/archive/2012/04/23/2466677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值