我的DirectX XAudio2程序出现了一个奇怪的问题。一切都应该工作正常,但我收到0xC0000005:访问冲突错误:
if(FAILED(hr = xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudioSource, lWaveFileFormat)))
这是XAudio2Manager.cpp:
#include "XAudio2Manager.h"
#include <exception>
class XAudio2InitException: public std::exception
{
virtual const char* what() const throw()
{
return "XAudio2 Initialiation failed";
}
} XAudio2ex;
template <class T> void SafeRelease(T **ppT)
{
if(*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
cAudioTrack::cAudioTrack()
{
mIsPlaying = false;
}
//cAudioTrack::~cAudioTrack();
//{
//}
void cAudioTrack::Play()
{
if(!mIsPlaying)
{
mAudioSource->Start();
mIsPlaying = true;
}
}
void cAudioTrack::Pause()
{
if(mIsPlaying)
{
mAudioSource->Stop();
mIsPlaying = false;
}
}
void cAudioTrack::Reset()
{
mAudioSource->FlushSourceBuffers();
mAudioSource->SubmitSourceBuffer(&buffer);
mIsPlaying = false;
}
cXAudio2Manager::cXAudio2Manager()
{
xAudio2Engine = NULL;
masterVoice = NULL;
HRESULT hr = S_OK;
// Create audio engine
if(SUCCEEDED(hr))
{
hr = XAudio2Create(&xAudio2Engine);
}
// Create mastering voice
if(SUCCEEDED(hr))
{
hr = xAudio2Engine->CreateMasteringVoice(&masterVoice,
XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE,
0,0, NULL);
}
// If failed, throw exception
if(!SUCCEEDED(hr))
{
throw XAudio2ex;
}
}
bool cXAudio2Manager::LoadPCMFile(LPWSTR filename, cAudioTrack *pAudioTrack, bool pLooping)
{
HRESULT hr = S_OK;
// Read wave file
CWaveFile lWaveFile;
if(FAILED(hr = lWaveFile.Open(const_cast<LPWSTR>(filename), nullptr, WAVEFILE_READ)))
{
return false;
}
// Get file format
WAVEFORMATEX* lWaveFileFormat = lWaveFile.GetFormat();
// Calculate bytes and samples
DWORD lWaveFileSize = lWaveFile.GetSize();
// Read sample data from wave file in to memory
BYTE* lWaveData = new BYTE[lWaveFileSize];
if(FAILED(hr = lWaveFile.Read(lWaveData, lWaveFileSize, &lWaveFileSize)))
{
if (lWaveData)
delete[] lWaveData;
return false;
}
if(FAILED(hr = xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudioSource, lWaveFileFormat)))
{
if(lWaveData)
delete [] lWaveData;
return false;
}
XAUDIO2_BUFFER buffer = {0};
pAudioTrack->buffer = buffer;
pAudioTrack->buffer.pAudioData = lWaveData;
pAudioTrack->buffer.Flags = XAUDIO2_END_OF_STREAM;
pAudioTrack->buffer.AudioBytes = lWaveFileSize;
if(pLooping)
{
pAudioTrack->buffer.LoopCount = XAUDIO2_LOOP_INFINITE;
}
if(FAILED(hr = pAudioTrack->mAudioSource->SubmitSourceBuffer(&pAudioTrack->buffer)))
{
pAudioTrack->mAudioSource->DestroyVoice();
if(lWaveData)
delete [] lWaveData;
return false;
}
return true;
}
在Game.cpp中调用Wave文件加载函数:
AudMa->LoadPCMFile(L"running.wav", mpCharacter->runningSound, true);
SDKwavefile正在使用中。
请帮忙!
答案 0 :(得分:1)
闻起来像是一个空指针。有问题的一行是:
xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudioSource, lWaveFileFormat)
因此,显而易见的可能性是xAudio2Engine
和pAudioTrack
。添加一些诊断程序以检查null。如果你找到null,那么深入挖掘以理解为什么会这样。