未设置DirectSound Wave格式

时间:2013-06-25 07:11:12

标签: directsound

我一直试图在我的程序中设置wave格式很长一段时间但没有成功......

它始终在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;
}

1 个答案:

答案 0 :(得分:1)

除非是拼写错误,否则以下行可能存在问题:

WaveFormat.nSamplesPerSec = 44,100;

应该是

WaveFormat.nSamplesPerSec = 44100L;