diff --git a/core/src/al/miniaudio_context.cpp b/core/src/al/miniaudio_context.cpp index e31842065e..5b1e0016d5 100644 --- a/core/src/al/miniaudio_context.cpp +++ b/core/src/al/miniaudio_context.cpp @@ -101,17 +101,23 @@ class MiniaudioSource : public impl::Source public: MiniaudioSource(ma_engine& engine) : mEngine(engine) + , mSoundInitialized(false) { } ~MiniaudioSource() override { ma_sound_uninit(&mSound); + mSoundInitialized = false; } void setBuffer(Buffer buffer) override { - ma_sound_uninit(&mSound); + if (mSoundInitialized) + { + ma_sound_uninit(&mSound); + mSoundInitialized = false; + } // Try to dynamically cast the Buffer to a MiniaudioBuffer. auto miniaudioBuffer = std::static_pointer_cast(buffer); @@ -121,6 +127,8 @@ class MiniaudioSource : public impl::Source CUBOS_ERROR("Failed to initialize sound from buffer"); return; } + + mSoundInitialized = true; } void setPosition(const glm::vec3& position) override @@ -211,6 +219,7 @@ class MiniaudioSource : public impl::Source private: ma_sound mSound; ma_engine& mEngine; + bool mSoundInitialized; }; class MiniaudioDevice : public impl::AudioDevice