我一直在安装xaudio2,虽然我认为我成功了,但我在声音仍在播放时退出了应用程序,程序崩溃了。最初我这样做是为了在收到WM_CLOSE消息时确保源语音已经停止。这大大减少了崩溃的次数,但在我退出应用程序时,程序仍然崩溃了大约五分之一。这就是它所说的";在Game.exe中的0x00007FF9974E38C7(XAudio2_9.dll(处引发异常:0xC0000005:读取位置0x0000025274A680C4的访问冲突;。我假设xaudio2线程仍然在尝试读取缓冲区,即使我已经处理了它。我已经以某种方式设置了它,这样就有了一个中央SoundSystem类,它有两个Voices向量(另一个类(,idleVoices和activeVoices。当我播放一个声音时,我从空闲频道中获取一个声音并播放一个音,然后当它完成时,我将其放回空闲频道并重复。
这是.h文件
#pragma once
#include <vector>
#include <memory>
#include <xaudio2.h>
#include <algorithm>
#include "Sound.h"
class SoundSystem
{
friend class Voice;
friend class Window;
public:
static SoundSystem& getInstance();
SoundSystem(SoundSystem&) = delete;
SoundSystem& operator=(SoundSystem&) = delete;
void playSound(Sound& sound, float frequency = 1.0f, float volume = 1.0f);
~SoundSystem();
private:
class Voice
{
friend class SoundSystem;
friend class Window;
public:
Voice()
{
}
Voice(SoundSystem* soundSystem);
void playSound(Sound& sound, float frequency = 1.0f, float volume = 1.0f);
void stop();
~Voice();
private:
class VoiceCallback : public IXAudio2VoiceCallback
{
public:
void STDMETHODCALLTYPE OnStreamEnd() override
{}
void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() override
{}
void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) override
{}
void STDMETHODCALLTYPE OnBufferEnd(void* pBufferContext) override;
void STDMETHODCALLTYPE OnBufferStart(void* pBufferContext) override
{}
void STDMETHODCALLTYPE OnLoopEnd(void* pBufferContext) override
{}
void STDMETHODCALLTYPE OnVoiceError(void* pBufferContext, HRESULT Error) override
{}
};
IXAudio2SourceVoice* sourceVoice = nullptr;
XAUDIO2_BUFFER buffer = { 0 };
};
void deactivateVoice(Voice* voice);
SoundSystem();
IXAudio2* audioEngine = nullptr;
IXAudio2MasteringVoice* masteringVoice = nullptr;
std::vector<std::unique_ptr<Voice>> idleVoices;
std::vector<std::unique_ptr<Voice>> activeVoices;
const unsigned int maxVoices = 256;
};
和.cpp文件
#include "SoundSystem.h"
#include "..Exception.h"
SoundSystem::Voice::Voice(SoundSystem* soundSystem)
{
HRESULT errorCode;
static VoiceCallback voiceCallback;
buffer.pContext = this;
if (FAILED(errorCode = soundSystem->audioEngine->CreateSourceVoice(&sourceVoice, &WaveFile::validFormat, 0, 2, &voiceCallback)))
{
throw Exception::AudioException("CreateSourceVoice failed", __FILE__, __LINE__, errorCode);
}
}
void SoundSystem::Voice::playSound(Sound& sound, float frequency, float volume)
{
HRESULT errorCode;
buffer.AudioBytes = sound.audioSize;
buffer.pAudioData = sound.audioBuffer;
if (FAILED(errorCode = sourceVoice->SubmitSourceBuffer(&buffer, nullptr)))
{
throw Exception::AudioException("SubmitSourceBuffer failed", __FILE__, __LINE__, errorCode);
}
if (FAILED(errorCode = sourceVoice->SetFrequencyRatio(frequency)))
{
throw Exception::AudioException("SetFrequencyRatio failed", __FILE__, __LINE__, errorCode);
}
if (FAILED(errorCode = sourceVoice->SetVolume(volume)))
{
throw Exception::AudioException("SetVolume failed", __FILE__, __LINE__, errorCode);
}
if (FAILED(errorCode = sourceVoice->Start()))
{
throw Exception::AudioException("Start failed", __FILE__, __LINE__, errorCode);
}
}
void SoundSystem::Voice::stop()
{
sourceVoice->Stop();
sourceVoice->FlushSourceBuffers();
}
SoundSystem::Voice::~Voice()
{
stop();
sourceVoice->DestroyVoice();
sourceVoice = nullptr;
}
void __stdcall SoundSystem::Voice::VoiceCallback::OnBufferEnd(void* pBufferContext)
{
Voice* voice = reinterpret_cast<Voice*>(pBufferContext);
voice->stop();
SoundSystem::getInstance().deactivateVoice(voice);
}
SoundSystem& SoundSystem::getInstance()
{
static SoundSystem instance;
return instance;
}
void SoundSystem::deactivateVoice(Voice* voice)
{
auto it = std::find_if(activeVoices.begin(), activeVoices.end(), [&](const std::unique_ptr<Voice>& v) -> bool
{
return voice = v.get();
});
idleVoices.push_back(std::move(*it));
activeVoices.erase(it);
}
SoundSystem::SoundSystem()
{
HRESULT errorCode;
if (FAILED(errorCode = CoInitializeEx(nullptr, COINITBASE_MULTITHREADED)))
{
throw Exception::AudioException("CoInitializeEx failed", __FILE__, __LINE__, errorCode);
}
if (FAILED(errorCode = XAudio2Create(&audioEngine, 0)))
{
throw Exception::AudioException("XAudio2Create failed", __FILE__, __LINE__, errorCode);
}
if (FAILED(errorCode = audioEngine->CreateMasteringVoice(&masteringVoice)))
{
throw Exception::AudioException("CreateMasteringVoice failed", __FILE__, __LINE__, errorCode);
}
for (int i = 0; i < maxVoices; i++)
{
idleVoices.push_back(std::make_unique<Voice>(this));
}
}
void SoundSystem::playSound(Sound& sound, float frequency, float volume)
{
if (idleVoices.size() > 0)
{
activeVoices.push_back(std::move(idleVoices.back()));
idleVoices.pop_back();
activeVoices.back()->playSound(sound, frequency, volume);
}
}
SoundSystem::~SoundSystem()
{
for (auto& a : idleVoices)
{
a.reset();
}
for (auto& a : activeVoices)
{
a.reset();
}
audioEngine->Release();
audioEngine = nullptr;
masteringVoice = nullptr;
CoUninitialize();
}
这是处理的WM_CLOSE消息
case WM_CLOSE:
for (auto& a : SoundSystem::getInstance().activeVoices)
{
a.get()->stop();
}
ApplicationEvent applicationEvent(ApplicationEventType::WindowClose);
EventSystem::getInstance().notify(&applicationEvent);
PostQuitMessage(0);
return 0;
break;
我认为关闭消息处理与此有关,因为停止源声音有所帮助,但它仍然崩溃。任何帮助都将不胜感激。
edit:我已经发现,在我收到WM_CLOSE消息后,我仍然能够有效地播放声音,从而消除我正在阻止它们的事实。有没有办法确保程序立即终止?
您应该在WM_CLOSE
中添加对StopEngine
的调用。这将停止对工作线程的所有处理,并可能解决您的崩溃问题。
您可能需要查看DirectX音频工具包。