我一直在尝试在我的程序中设置波形格式已经有一段时间了,但没有成功......
它总是在 directsound->setFormat 方法中返回 false ......有人可以帮我吗?
下面发布的是我正在处理的代码
bool SoundClass::InitializeDirectSound(HWND HANDLE)
{
HRESULT hr;
DSBUFFERDESC BufferDesc;
WAVEFORMATEX WaveFormat;
hr = DirectSoundCreate8(NULL,&m_pDirectSound,NULL);
if(FAILED(hr))
{
return false;
}
hr = m_pDirectSound->SetCooperativeLevel(HANDLE,DSSCL_PRIORITY);
if(FAILED(hr))
{
return false;
}
//setup primary buffer desc
BufferDesc.dwSize = sizeof(DSBUFFERDESC);
BufferDesc.dwBufferBytes = 0;
BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
BufferDesc.guid3DAlgorithm = GUID_NULL;
BufferDesc.dwReserved = 0;
BufferDesc.lpwfxFormat = NULL;
// Get control of the primary sound buffer on the default sound device.
hr = m_pDirectSound->CreateSoundBuffer(&BufferDesc,&m_pPrimarySoundBuffer,NULL);
if(FAILED(hr))
{
return false;
}
//setup the format of primary buffer
// In this case it is a .WAV file recorded at 44,100 samples per second in 16-bit stereo (cd audio format).
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nSamplesPerSec = 44,100;
WaveFormat.wBitsPerSample = 16;
WaveFormat.nChannels = 2;
WaveFormat.nBlockAlign =(WaveFormat.wBitsPerSample / 8) * WaveFormat.nChannels;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
WaveFormat.cbSize = 0;
//set the primary buffer format to the described waveformat(it always returns false here)
hr = m_pPrimarySoundBuffer->SetFormat(&WaveFormat);
if(FAILED(hr))
{
return false;
}
return true;
}